DataGrid 保存按钮和 CanExecute
我在 XAML 中有一个数据网格和保存按钮。我有一个绑定到数据网格的 ObservableCollection。
如果我在数据网格中添加/删除一行,我应该能够启用“保存”按钮以允许用户保存记录。然而,ObservableCollection 的 NotifyCollectionChangedAction 无法捕获“编辑”(即值更改)。因此,我想在调用数据网格的 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?
}
I have a datagrid and save button in XAML. I have a ObservableCollection bound to a datagrid.
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' (i.e. value changes). So I want to manually enable the save button when the datagrid's currentcellchanged event is invoked (i.e. set e.CanExecute = true).
Since it's not like you can set enable=true as in WinForms, WPF has this CanExecute and Executed command binding.
In my 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?
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我添加了一个触发器,当我完成编辑单元格时,设置 bool Edited= 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.
i don't know if it's the best, but it works for me.