C# 如何使用MVVM规则处理CommandBinding的执行事件?

C# 如何使用MVVM规则处理CommandBinding的执行事件?,c#,wpf,events,mvvm,event-handling,C#,Wpf,Events,Mvvm,Event Handling,我有一个DataGrid,我想关闭DataGrid上的SelectAll方法。准确地说,此按钮: 以下是将在代码隐藏中处理的已执行事件: <DataGrid ItemsSource="{Binding EmployeeDataTable}" Name="dataGrid"> <DataGrid.CommandBindings> <CommandBinding Command="ApplicationCommands.S

我有一个DataGrid,我想关闭DataGrid上的SelectAll方法。准确地说,此按钮:

以下是将在代码隐藏中处理的已执行事件:

   <DataGrid ItemsSource="{Binding EmployeeDataTable}" Name="dataGrid">
        <DataGrid.CommandBindings>
            <CommandBinding Command="ApplicationCommands.SelectAll" 
                                           Executed="SelectAll_Executed">
            </CommandBinding>
        </DataGrid.CommandBindings>            
    </DataGrid>
private void SelectAll_Executed(object sender, ExecutedRoutedEventArgs e)
{
   //dataGrid.SelectAll();//I commented this line so user cannot select all rows 
   //in a datagrid         
}
上面的代码非常有效,用户无法选择DataGrid中的所有行,这就是我想要的。但我想将eventSelectAll_执行的处理程序移动到viewModel

我所尝试的:

<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>
但我遇到过这样的错误:

附加的属性触发器只能应用于从DependencyObject派生的类型。DataGridAddedColumns

如何使用MVVM规则处理CommandBinding的已执行事件?

ApplicationCommand.SelectAll是什么/在哪里?我认为您可能希望在VM中为所选项目创建一个属性,该属性除了完整的项目列表外,还将绑定到DataGrid——这样,您就可以对所选的任何内容执行逻辑,无论是用户通过Ctrl+单击手动选择的所有项目还是行。您可以在这里找到一些有用的信息:@bdimag ApplicationCommands.SelectAll允许选择DataGrid中的所有行。ApplicationCommand.SelectAll绑定到图像中显示的按钮。现在我正在尝试处理SelectionChanged事件并计算SelectedItems:。ApplicationCommand.SelectAll是什么/在哪里?我认为您可能希望在VM中为所选项目创建一个属性,该属性除了完整的项目列表外,还将绑定到DataGrid——这样,您就可以对所选的任何内容执行逻辑,无论是用户通过Ctrl+单击手动选择的所有项目还是行。您可以在这里找到一些有用的信息:@bdimag ApplicationCommands.SelectAll允许选择DataGrid中的所有行。ApplicationCommand.SelectAll绑定到图像中显示的按钮。现在我正在尝试处理SelectionChanged事件并计算SelectedItems:。