title: EventToCommand for SelectionChanged on ComboBox inside of DataGrid mvvm/wpf
body: <p>How can I catch the Selection Changed event that fires on a <code>ComboBox</code> that is embedded into a <code>DataGridComboBoxColum</code>? I would like to use MVVM pattern for this so something like a <code>EventToCommmand</code> solution would be nice. </p>
  
  <p>XAML:</p>
  
  <pre><code>&lt;DataGridComboBoxColumn Header="ViewTemplate" Width="180" SelectedItemBinding="{Binding ViewTemplate}" DisplayMemberPath="Name"&gt;
      &lt;DataGridComboBoxColumn.ElementStyle&gt;
          &lt;Style TargetType="{x:Type ComboBox}" BasedOn="{StaticResource {x:Type ComboBox}}"&gt;
              &lt;Setter Property="ItemsSource" Value="{Binding DataContext.ViewTemplates, RelativeSource={RelativeSource AncestorType=DataGrid}}"/&gt;
              &lt;Setter Property="IsReadOnly" Value="True"/&gt;
          &lt;/Style&gt;
      &lt;/DataGridComboBoxColumn.ElementStyle&gt;
      &lt;DataGridComboBoxColumn.EditingElementStyle&gt;
          &lt;Style TargetType="{x:Type ComboBox}" BasedOn="{StaticResource {x:Type ComboBox}}"&gt;
              &lt;Setter Property="ItemsSource" Value="{Binding DataContext.ViewTemplates, RelativeSource={RelativeSource AncestorType=DataGrid}}"/&gt;
          &lt;/Style&gt;
      &lt;/DataGridComboBoxColumn.EditingElementStyle&gt;
  &lt;/DataGridComboBoxColumn&gt;
  </code></pre>
  
  <p>I would like to use something like this but don't know where/how to set it up in this particular case. </p>
  
  <pre><code>&lt;i:Interaction.Triggers&gt;
      &lt;i:EventTrigger EventName="SelectionChanged"&gt;
          &lt;cmd:EventToCommand PassEventArgsToCommand="True" Command="{Binding SelectionChangedCommand}"/&gt;
      &lt;/i:EventTrigger&gt;
  &lt;/i:Interaction.Triggers&gt;
  </code></pre>
  
  <p>As requested this is my full code for XAML: </p>
  
  <pre><code>&lt;UserControl x:Class="GrimshawRibbon.Revit.Views.ViewManager.ViewManagerView"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
               xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
               xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
               xmlns:local="clr-namespace:GrimshawRibbon.Revit.Views.ViewManager"
               xmlns:ex="clr-namespace:GrimshawRibbon.Revit.Wpf.Extensions"
               xmlns:controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
               xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Platform"
               mc:Ignorable="d" 
               d:DesignHeight="300" d:DesignWidth="500"&gt;
      &lt;UserControl.Resources&gt;
          &lt;ResourceDictionary&gt;
              &lt;ResourceDictionary.MergedDictionaries&gt;
                  &lt;ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" /&gt;
                  &lt;ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" /&gt;
                  &lt;ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" /&gt;
                  &lt;ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" /&gt;
                  &lt;ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" /&gt;
                  &lt;ResourceDictionary Source="pack://application:,,,/GrimshawRibbon;component/Revit/Wpf/Style/GrimshawTheme.xaml"/&gt;
              &lt;/ResourceDictionary.MergedDictionaries&gt;
          &lt;/ResourceDictionary&gt;
      &lt;/UserControl.Resources&gt;
      &lt;Grid&gt;
          &lt;ex:DataGridEx x:Name="dgViews" 
                                 Style="{StaticResource AzureDataGrid}" 
                                 Margin="10" 
                                 AutoGenerateColumns="False" 
                                 ItemsSource="{Binding Views, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                                 CanUserAddRows="False" 
                                 IsReadOnly="False" 
                                 SelectionMode="Extended" 
                                 SelectionUnit="FullRow" 
                                 SelectedItemsList="{Binding SelectedViews, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"&gt;
              &lt;i:Interaction.Triggers&gt;
                  &lt;i:EventTrigger EventName="CellEditEnding"&gt;
                      &lt;cmd:EventToCommand PassEventArgsToCommand="True" Command="{Binding CellEditEndingCommand}"/&gt;
                  &lt;/i:EventTrigger&gt;
              &lt;/i:Interaction.Triggers&gt;
              &lt;DataGrid.Columns&gt;
                  &lt;DataGridTextColumn Header="Name" Binding="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="*"/&gt;
                  &lt;DataGridTextColumn Header="ViewType" Binding="{Binding ViewType}" Width="100" IsReadOnly="True"/&gt;
                  &lt;DataGridTextColumn Header="ViewGroup" Binding="{Binding ViewGroup, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="130" IsReadOnly="False"/&gt;
                  &lt;DataGridTextColumn Header="ViewSubGroup" Binding="{Binding ViewSubGroup, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="130" IsReadOnly="False"/&gt;
                  &lt;DataGridCheckBoxColumn ElementStyle="{DynamicResource MetroDataGridCheckBox}" 
                                          EditingElementStyle="{DynamicResource MetroDataGridCheckBox}" 
                                          Header="OnSheet" 
                                          Binding="{Binding OnSheet, Mode=TwoWay}" 
                                          IsReadOnly="True" 
                                          Width="80"&gt;
                  &lt;/DataGridCheckBoxColumn&gt;
                  &lt;DataGridComboBoxColumn Header="ViewTemplate" Width="180" SelectedItemBinding="{Binding ViewTemplate}" DisplayMemberPath="Name"&gt;
                      &lt;i:Interaction.Triggers&gt;
                          &lt;i:EventTrigger EventName="SelectionChanged"&gt;
                              &lt;cmd:EventToCommand PassEventArgsToCommand="True" Command="{Binding DataContext.SelectChangeCommand, RelativeSource={RelativeSource AncestorType=UserControl}}"/&gt;
                          &lt;/i:EventTrigger&gt;
                      &lt;/i:Interaction.Triggers&gt;
                      &lt;DataGridComboBoxColumn.ElementStyle&gt;
                          &lt;Style TargetType="{x:Type ComboBox}" BasedOn="{StaticResource {x:Type ComboBox}}"&gt;
                              &lt;Setter Property="ItemsSource" Value="{Binding DataContext.ViewTemplates, RelativeSource={RelativeSource AncestorType=DataGrid}}"/&gt;
                              &lt;Setter Property="IsReadOnly" Value="True"/&gt;
                          &lt;/Style&gt;
                      &lt;/DataGridComboBoxColumn.ElementStyle&gt;
                      &lt;DataGridComboBoxColumn.EditingElementStyle&gt;
                          &lt;Style TargetType="{x:Type ComboBox}" BasedOn="{StaticResource {x:Type ComboBox}}"&gt;
                              &lt;Setter Property="ItemsSource" Value="{Binding DataContext.ViewTemplates, RelativeSource={RelativeSource AncestorType=DataGrid}}"/&gt;
                          &lt;/Style&gt;
                      &lt;/DataGridComboBoxColumn.EditingElementStyle&gt;
                  &lt;/DataGridComboBoxColumn&gt;
              &lt;/DataGrid.Columns&gt;
          &lt;/ex:DataGridEx&gt;
      &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>and view model: </p>
  
  <pre><code>public class ViewManagerViewModel : ViewModelBaseEx
  {
      public ViewManagerModel Model;
      public ObservableCollection&lt;ViewWrapper&gt; Views { get; private set; }
      public ObservableCollection&lt;ViewWrapper&gt; ViewTemplates { get; private set; }
      public IList SelectedViews { get; set; }
      public RelayCommand&lt;DataGridCellEditEndingEventArgs&gt; CellEditEndingCommand { get; set; }
      public RelayCommand&lt;SelectionChangedEventArgs&gt; SelectChangeCommand { get; set; }
  
      public ViewManagerViewModel(Document doc)
      {
          Model = new ViewManagerModel(doc);
          Views = Model.CollectViews();
          ViewTemplates = Model.CollectViewTemplates();
          CellEditEndingCommand = new RelayCommand&lt;DataGridCellEditEndingEventArgs&gt;(args =&gt; OnCellEditEnding(args));
          SelectChangeCommand = new RelayCommand&lt;SelectionChangedEventArgs&gt;(args =&gt; OnSelectionChanged(args));
      }
  
      private void OnSelectionChanged(SelectionChangedEventArgs e)
      {
          // do something
      }
  
      /// &lt;summary&gt;
      /// Logic for handling cell editing events.
      /// &lt;/summary&gt;
      /// &lt;param name="e"&gt;DataGrid Row object.&lt;/param&gt;
      private void OnCellEditEnding(DataGridCellEditEndingEventArgs e)
      {
          var wrapper = e.Row.Item as ViewWrapper;
          switch (e.Column.SortMemberPath)
          {
              case "Name":
                  Model.ChangeName(wrapper);
                  break;
              case "ViewGroup":
                  Model.SetParameter(wrapper, "View Group", wrapper.ViewGroup);
                  break;
              case "ViewSubGroup":
                  Model.SetParameter(wrapper, "View Sub Group", wrapper.ViewSubGroup);
                  break;
              default:
                  break;
          }
      }
  
      public override void Apply()
      {
          var vw = new List&lt;ViewWrapper&gt;();
          foreach (ViewWrapper v in SelectedViews)
          {
              vw.Add(v);
          }
  
          Model.Delete(vw);
  
          // update collection so that rows get deleted in datagrid
          foreach (ViewWrapper i in vw)
          {
              Views.Remove(i);
          }
      }
  }
  </code></pre>