DataGrid 保存按钮和 CanExecute

发布于 2024-11-09 15:36:13 字数 1278 浏览 3 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稍尽春風 2024-11-16 15:36:13

我添加了一个触发器,当我完成编辑单元格时,设置 bool Edited= true 并回发,保存按钮将捕获更改并将其自身设置为启用。

我不知道这是否是最好的,但它对我有用。

private void Save_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = Edited;
        }

 private void businessDataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {
            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.

private void Save_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = Edited;
        }

 private void businessDataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {
            Edited = true;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文