MVVM 环境中的 WPF Datagrid RowEditEnding

发布于 2024-12-03 12:26:21 字数 699 浏览 0 评论 0原文

我一直在尝试使用以下方法捕获用户输入数据网格的值

<b:Interaction.Triggers>
    <b:EventTrigger EventName="RowEditEnding">
        <b:InvokeCommandAction  Command="{Binding ReleaseRowEditEndingCommand}" CommandParameter="{Binding SelectedRelease}"/>
    </b:EventTrigger>

但这不起作用,我现在在阅读 this 后明白了这一点StackOverflow 上的文章。所提出的解决方案似乎都是基于直接调用与引发的事件相匹配的方法签名,在这种情况下,

private void OnRowEditEnding(object sender, DataGridRowEditEndingEventArgs e)

是否有人在 MVVM 情况下完成了在 rowedit 后获取值?所有解决方案似乎都将事件紧密绑定到 XAML,如果可能的话,我想避免这种情况。

I've been trying to capture the values that a user enters into a datagrid by using

<b:Interaction.Triggers>
    <b:EventTrigger EventName="RowEditEnding">
        <b:InvokeCommandAction  Command="{Binding ReleaseRowEditEndingCommand}" CommandParameter="{Binding SelectedRelease}"/>
    </b:EventTrigger>

But this doesn't work and I now understand this afte reading this article on StackOverflow. The solutions presented all seem to be based on directly calling a method signature that matches the event being raised, in this case

private void OnRowEditEnding(object sender, DataGridRowEditEndingEventArgs e)

Has anyone accomplished getting the values post-rowedit in an MVVM situation? All the solutions seem to tightly bind the event to the XAML and I'd like to avoid that, if possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

单身狗的梦 2024-12-10 12:26:21

解决方案实际上比我想象的要容易。我更改了 XAML,现在可以获取视图模型上 RowEditEnding 事件中的值。这是数据网格上数据列的之前:

 <DataGrid.Columns>
    <DataGridCheckBoxColumn Header="Is Paid" Binding="{Binding IsPaid, Mode=TwoWay}" />
    <DataGridTextColumn Header="Amount" Binding="{Binding Amount, Mode=TwoWay}" />
</DataGrid.Columns>

之后

 <DataGrid.Columns>
    <DataGridCheckBoxColumn Header="Is Paid" Binding="{Binding IsPaid, UpdateSourceTrigger=PropertyChanged}" />
    <DataGridTextColumn Header="Amount" Binding="{Binding Amount,  UpdateSourceTrigger=PropertyChanged}" />
</DataGrid.Columns>

The solution was actually easier than I thought. I changed the XAML and I can now get the values in the RowEditEnding event on the View Model. Here's the before on the data columns on the datagrid:

 <DataGrid.Columns>
    <DataGridCheckBoxColumn Header="Is Paid" Binding="{Binding IsPaid, Mode=TwoWay}" />
    <DataGridTextColumn Header="Amount" Binding="{Binding Amount, Mode=TwoWay}" />
</DataGrid.Columns>

After

 <DataGrid.Columns>
    <DataGridCheckBoxColumn Header="Is Paid" Binding="{Binding IsPaid, UpdateSourceTrigger=PropertyChanged}" />
    <DataGridTextColumn Header="Amount" Binding="{Binding Amount,  UpdateSourceTrigger=PropertyChanged}" />
</DataGrid.Columns>
ヤ经典坏疍 2024-12-10 12:26:21

您可以尝试将数据包装在 ListCollectionView 中并将 DataGrid 绑定到 ListCollectionView。然后,在 ViewModel 中,挂钩 ListCollectionView.CurrentChanging 事件以在移动到新行之前处理更改。

You could try wrapping your data in a ListCollectionView and binding the DataGrid to the ListCollectionView. Then, in your ViewModel, hook into the ListCollectionView.CurrentChanging event to process your changes before moving to the new row.

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