[英]DataGrid save button and CanExecute
I have a datagrid and save button in XAML.我在 XAML 中有一个数据网格和保存按钮。 I have a ObservableCollection bound to a datagrid.
我有一个绑定到数据网格的 ObservableCollection。
If I add/remove a row in datagrid, I should be able to enable the 'Save' Button to allow the user to save records.如果我在数据网格中添加/删除一行,我应该能够启用“保存”按钮以允许用户保存记录。 However the ObservableCollection's NotifyCollectionChangedAction can't catch the 'edit' (ie value changes).
但是 ObservableCollection 的 NotifyCollectionChangedAction 无法捕捉到“编辑”(即值更改)。 So I want to manually enable the save button when the datagrid's currentcellchanged event is invoked (ie set e.CanExecute = true).
所以我想在调用datagrid 的currentcellchanged 事件时手动启用保存按钮(即设置e.CanExecute = true)。
Since it's not like you can set enable=true as in WinForms, WPF has this CanExecute and Executed command binding.由于您不能像在 WinForms 中那样设置 enable=true,因此 WPF 具有此 CanExecute 和 Executed 命令绑定。
In my XAML:在我的 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" >
Code:代码:
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?
}
i added a trigger, when i finish editing cell, set the bool Edited= true and postback, the save button will catch the change and set itself enable.我添加了一个触发器,当我完成编辑单元格时,设置 bool Edited= true 并回发,保存按钮将捕获更改并将其自身设置为启用。
i don't know if it's the best, but it works for me.我不知道它是否是最好的,但它对我有用。
private void Save_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = Edited;
}
private void businessDataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
Edited = true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.