title: How to take a WPF CommandParameter to the ViewModel in MVVM?
body: <p>I hope somebody can help me out here. Simplified the code for posting.</p>
  
  <p>We have a main window  (MvvmTestView) with a menu, and a 2nd window (SettingsView) which holds several tabs. I can open the SettingsView window alright. I can even select which Tab to open by setting this in the code.</p>
  
  <p>How can I get back the correct value with the command parameter from the XAML code so that the correct tab opens?</p>
  
  <p>MvvmTestView.xaml:</p>
  
  <pre><code>&lt;Window x:Class="MvvmTest.Views.MvvmTestView"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:vm="clr-namespace:MvvmTest.ViewModels"
      WindowStartupLocation="CenterScreen"
      Title="MvvmTestView" 
      Height="500" 
      Width="500"&gt;
  
      &lt;Window.DataContext&gt;
          &lt;vm:MvvmTestViewModel/&gt;
      &lt;/Window.DataContext&gt;
      &lt;Grid&gt;
          &lt;DockPanel&gt;
              &lt;Menu&gt;
                  &lt;MenuItem Header="Menu"&gt;
                      &lt;MenuItem 
                          Header="Tab01" 
                          Command="{Binding SettingsViewCommand}"
                          CommandParameter="0"/&gt;
                      &lt;MenuItem
                          Header="Tab02"
                          Command="{Binding SettingsViewCommand}"
                          CommandParameter="1"/&gt;
                  &lt;/MenuItem&gt;
              &lt;/Menu&gt;
          &lt;/DockPanel&gt;
          &lt;DockPanel&gt;
              &lt;Label Content="MainView" HorizontalAlignment="Center" VerticalAlignment="Center"/&gt;
          &lt;/DockPanel&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>SettingView.xaml</p>
  
  <pre><code>&lt;Window x:Class="MvvmTest.Views.SettingsView"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:tabData="clr-namespace:MvvmTest.Views"
      xmlns:vm="clr-namespace:MvvmTest.ViewModels"
      WindowStartupLocation="CenterScreen"
      Title="SettingsView" 
      Height="400"
      Width="400"&gt;
  
      &lt;Window.DataContext&gt;
          &lt;vm:MvvmTestViewModel/&gt;
      &lt;/Window.DataContext&gt;
      &lt;Grid&gt;
          &lt;TabControl
              SelectedIndex="{Binding SettingsSelectedIndex, Mode=TwoWay}"&gt;
              &lt;tabData:Tab01View/&gt; 
              &lt;tabData:Tab02View/&gt;
          &lt;/TabControl&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>SettingsViewModel.cs</p>
  
  <pre><code>using System.ComponentModel;
  
  namespace MvvmTest.ViewModels
  {
      public class SettingsViewModel : INotifyPropertyChanged
      {
          public event PropertyChangedEventHandler PropertyChanged;
  
          private void OnPropertyChanged(string property)
          {
              PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
          }
  
          private int _settingsSelectedIndex;
          public int SettingsSelectedIndex
          {
              get
              {
                  return _settingsSelectedIndex;
              }
              set
              {
                  _settingsSelectedIndex = value;
                  OnPropertyChanged("SettingsSelectedIndex");
              }
          }
      }
  }
  </code></pre>
  
  <p>MvvmTestViewModel.cs</p>
  
  <pre><code>using MvvmTest.Commands;
  using MvvmTest.Views;
  using System.ComponentModel;
  using System.Windows.Input;
  
  namespace MvvmTest.ViewModels
  {
      internal class MvvmTestViewModel : INotifyPropertyChanged
      {
          public event PropertyChangedEventHandler PropertyChanged;
  
          private void OnPropertyChanged(string propertyName)
          {
              PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
          }
  
          private SettingsViewModel SettingsViewModel;
  
          public MvvmTestViewModel()
          {
              SettingsViewModel = new SettingsViewModel();
              SettingsViewCommand = new SettingsViewCommand(this);
          }
  
          public ICommand SettingsViewCommand
          {
              get;
              private set;
          }
  
          public void SettingsWindow()
          {
              SetIndex();
              SettingsView settingsView = new SettingsView()
              {
                  DataContext = SettingsViewModel
              };
              settingsView.ShowDialog();
          }
  
          public int SetIndex()
          {
              SettingsViewModel.SettingsSelectedIndex = 1;
              return SettingsViewModel.SettingsSelectedIndex;
          }
      }
  }
  </code></pre>
  
  <p>SettingsViewCommand.cs</p>
  
  <pre><code>using MvvmTest.ViewModels;
  using System;
  using System.Windows.Input;
  
  namespace MvvmTest.Commands
  {
      internal class SettingsViewCommand : ICommand
      {
          private MvvmTestViewModel settingsViewModel;
  
          public SettingsViewCommand(MvvmTestViewModel settingsViewModel)
          {
              this.settingsViewModel = settingsViewModel;
          }
  
          public event EventHandler CanExecuteChanged
          {
              add
              {
                  CommandManager.RequerySuggested += value;
              }
              remove
              {
                  CommandManager.RequerySuggested -= value;
              }
          }
  
          public bool CanExecute(object parameter)
          {
              return true;
          }
  
          public void Execute(object parameter)
          {
              settingsViewModel.SettingsWindow();
          }
      }
  }
  </code></pre>