1

I have a DataGrid and I want to turn off SelectAll() method on DataGrid.
To be precise this button: enter image description here

Here is Executed event which will be handled in code behind:

   <DataGrid ItemsSource="{Binding EmployeeDataTable}" Name="dataGrid">
        <DataGrid.CommandBindings>
            <CommandBinding Command="ApplicationCommands.SelectAll" 
                                           Executed="SelectAll_Executed">
            </CommandBinding>
        </DataGrid.CommandBindings>            
    </DataGrid>

code behind:

private void SelectAll_Executed(object sender, ExecutedRoutedEventArgs e)
{
   //dataGrid.SelectAll();//I commented this line so user cannot select all rows 
   //in a datagrid         
}

The above code perfectly works and a user cannot select all rows in DataGrid and that is what I want. But I want to move handler of event(SelectAll_Executed) to viewModel.

What I've tried:

<DataGrid ItemsSource="{Binding EmployeeDataTable}" Name="dataGrid">
        <DataGrid.CommandBindings>
            <CommandBinding Command="ApplicationCommands.SelectAll" >
               <i:Interaction.Triggers>
                    <i:EventTrigger EventName="Executed">
                        <i:InvokeCommandAction Command="{Binding Path=SomeCommand, Mode=OneWay}"/>
                    </i:EventTrigger>
               </i:Interaction.Triggers>
            </CommandBinding>
        </DataGrid.CommandBindings>            
    </DataGrid>

But I've met such error:

The attached property "Triggers" can only be applied to types that are derived from "DependencyObject". DataGridAddedColumns

How to handle Executed event of CommandBinding using MVVM rules?

StepUp
  • 36,391
  • 15
  • 88
  • 148
  • 1
    What/where is `ApplicationCommands.SelectAll`? I would think you'd want a property in the VM for selected items and that would be bound to the DataGrid in addition to the full list of items -- this way, you can perform logic on whatever is selected, whether it be all items or rows manually selected by the user with Ctrl+Click. You may find some useful information here: http://stackoverflow.com/questions/22868445/wpf-binding-selecteditems-in-mvvm – bdimag May 04 '16 at 18:47
  • @bdimag `ApplicationCommands.SelectAll` allows to select all rows in `DataGrid`. `ApplicationCommands.SelectAll` is binded to the button shown in the image. Now I am trying to handle `SelectionChanged` event and count `SelectedItems`:). – StepUp May 04 '16 at 18:54

0 Answers0