2
我有一个数据网格和保存按钮在XAML。我有一个ObservableCollection绑定到一个数据网格。DataGrid保存按钮和CanExecute
如果我在datagrid中添加/删除一行,我应该可以启用“保存”按钮来允许用户保存记录。但是ObservableCollection的NotifyCollectionChangedAction不能捕获'编辑'(即值更改)。所以我想在调用datagrid的currentcellchanged事件时手动启用保存按钮(即set 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?
}