title: Creating abstract ViewModels wpf
body: <p>I'm trying to create an abstract ViewModel class, and several ViewModel classes which will inherit the abstract ViewModel and implement it.</p>
  
  <p>So far I'm using <code>RelayCommand</code> and it doesn't compile.</p>
  
  <p>Can such a thing be done?</p>
  
  <p>I'm adding my code:</p>
  
  <p><strong>RelayCommand class:</strong></p>
  
  <pre><code>public class RelayCommand : ICommand
  {
      private readonly Action&lt;object&gt; m_executeAction;
      private readonly Predicate&lt;object&gt; m_canExecute;
  
      public RelayCommand(Action&lt;object&gt; executeAction) : this(executeAction, null) { }
  
      public RelayCommand(Action&lt;object&gt; executeAction, Predicate&lt;object&gt; canExecute)
      {
          if (executeAction == null)
              throw new ArgumentNullException("executeAction");
  
          m_executeAction = executeAction;
          m_canExecute = canExecute;
      }
  
      public bool CanExecute(object canExecuteParameter)
      {
          return (m_canExecute == null || m_canExecute(canExecuteParameter));
      }
  
      public event EventHandler CanExecuteChanged
      {
          add { CommandManager.RequerySuggested += value; }
          remove { CommandManager.RequerySuggested -= value; }
      }
  
      public void Execute(object canExecuteParameter)
      {
          m_executeAction(canExecuteParameter);
      }
  }
  </code></pre>
  
  <p><strong>The ViewModelsBase class:</strong></p>
  
  <pre><code>public abstract class ViewModelBase : INotifyPropertyChanged, IDisposable
  {
      public event PropertyChangedEventHandler PropertyChanged;
  
      public void OnPropertyChanged(string propertyName)
      {
          if (PropertyChanged == null) return;
          PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
      }
  
      protected virtual void OnDispose() {}
  
      public void Dispose()
      {
          OnDispose();
      }
  }
  </code></pre>
  
  <p><strong>The MainViewModel class:</strong></p>
  
  <pre><code>public class MainWindowViewModel : ViewModelBase
  {
      private IViewModel m_testViewModel;
      private bool m_isFirstPlugin = true;
  
      public MainWindowViewModel()
      {
          TestViewModel = new FirstViewModel();
      }
  
      public IViewModel TestViewModel
      {
          get { return m_testViewModel; }
          set
          {
              m_testViewModel = value;
              OnPropertyChanged("NewViewModel");
          }
      }
  
      private ICommand m_changeCommand;
  
      public ICommand ChangeCommand
      {
          get { return m_changeCommand ?? (m_changeCommand = new RelayCommand(Change)); }
          set { m_changeCommand = value; }
      }
  
      private void Change(object parameter)
      {
          TestViewModel.Dispose();
          TestViewModel = null;
  
          if (m_isFirstPlugin)
              TestViewModel = new SecondViewModel();
          else
              TestViewModel = new FirstViewModel();
  
          m_isFirstPlugin = !m_isFirstPlugin;
      }
  }
  </code></pre>
  
  <p><strong>IViewModel class:</strong></p>
  
  <pre><code>public class IViewModel : ViewModelBase
  {
      private ICommand m_testCommand;
  
      public ICommand TestCommand
      {
          get { return m_testCommand ?? (m_testCommand = new RelayCommand(Test)); }
          set { m_testCommand = value; }
      }
  
      protected virtual void Test(object parameter) { }
  }
  </code></pre>
  
  <p><strong>FirstViewModel class:</strong></p>
  
  <pre><code>public class FirstViewModel : IViewModel
  {
      protected override void Test(object parameter)
      {
          MessageBox.Show("On First Plugin.");
      }
  }
  </code></pre>
  
  <p><strong>SecondViewModel class:</strong></p>
  
  <pre><code>public class SecondViewModel : IViewModel
  {
      protected override void Test(object parameter)
      {
          MessageBox.Show("On Second Plugin.");
      }
  }
  </code></pre>
  
  <p><strong>Xaml:</strong></p>
  
  <pre><code>&lt;Window x:Class="MvvmInheritence.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Testing" Height="100" Width="180"
      xmlns:Local="clr-namespace:MvvmInheritence" WindowStartupLocation="CenterScreen" Background="Transparent"&gt;
      &lt;Window.DataContext&gt;
          &lt;Local:MainWindowViewModel /&gt;
      &lt;/Window.DataContext&gt;
      &lt;Grid x:Name="MainGrid"&gt;
          &lt;Grid.RowDefinitions&gt;
              &lt;RowDefinition /&gt;
              &lt;RowDefinition /&gt;
          &lt;/Grid.RowDefinitions&gt;
          &lt;Button x:Name="TestButton" Content="Test!" Foreground="DarkRed" Background="LightBlue"  Height="25" Width="100" Command="{Binding TestCommand, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" DataContext="{Binding TestViewModel, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Row="0"/&gt;
          &lt;Button x:Name="ChangeButton" Content="Change Plugin" Foreground="DarkRed" Background="LightBlue"  Height="25" Width="100" Command="{Binding ChangeCommand, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Row="1"/&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>This code does compile (I made changes to make it work) but for some reason, I'm always getting "On First Plugin" even though the <code>Change</code> function is called, and the <code>ViewModel</code> is correctly changed.</p>
  
  <p>What am I missing?</p>