我在 XAML 中有一个数据网格和保存按钮。我有一个绑定到数据网格的 ObservableCollection。
如果我在数据网格中添加/删除一行,我应该能够启用“保存”按钮以允许用户保存记录。但是 ObservableCollection 的 NotifyCollectionChangedAction 无法捕捉到“编辑”(即值更改)。所以我想在调用datagrid 的currentcellchanged 事件时手动启用保存按钮(即设置e.CanExecute = true)。
由于您不能像在 WinForms 中那样设置 enable=true,因此 WPF 具有此 CanExecute 和 Executed 命令绑定。
在我的 XAML 中:
</UserControl.Resources>
<UserControl.CommandBindings>
<CommandBinding Command="Save" Executed="Save_Executed" CanExecute="Save_CanExecute">
</CommandBinding>
</UserControl.CommandBindings>
<Button Grid.Row="4" Content="Save" Command="Save" HorizontalAlignment="Right" Margin="5" Name="saveButton" VerticalAlignment="Center" Width="75" >
代码:
private void Save_Executed(object sender, ExecutedRoutedEventArgs e)
{
}
private void Save_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = businessContractorViewModel != null && businessContractorViewModel.Entry != null;
}
private void businessDataGrid_CurrentCellChanged(object sender, EventArgs e)
{
//?? how to set savebutton e.canexecute = true?
}