WPF DataGrid CellEditEnded 事件 [英] WPF DataGrid CellEditEnded event

查看:38
本文介绍了WPF DataGrid CellEditEnded 事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次用户编辑我的 DataGrid 单元格的内容时,我都想知道.有 CellEditEnding 事件,但在对 DataGrid 绑定到的集合进行任何更改之前调用它.

I'm looking to know every time the user has edited a content of my DataGrid's cell. There's CellEditEnding event, but its called before any changes were made to the collection, that the DataGrid is bound to.

我的数据网格绑定到 ObservableCollection,其中 Item 是一个类,从 WCF mex 端点自动生成.

My datagrid is bound to ObservableCollection<Item>, where Item is a class, automatically generated from WCF mex endpoint.

了解用户每次对集合进行更改的最佳方式是什么.

What is the best way to know every time the user has committed the changes to the collection.

更新

我已经尝试过 CollectionChanged 事件,当 Item 被修改时它不会被触发.

I've tried CollectionChanged event, end it does not get triggered when Item gets modified.

推荐答案

您可以在数据网格的属性成员绑定上使用 UpdateSourceTrigger=PropertyChanged.这将确保当 CellEditEnding 被触发时,更新已经反映在 observable 集合中.

You can use UpdateSourceTrigger=PropertyChangedon the binding of the property member for the datagrid. This will ensure that when CellEditEnding is fired the update has already been reflected in the observable collection.

见下文

<DataGrid SelectionMode="Single"
          AutoGenerateColumns="False"
          CanUserAddRows="False"
          ItemsSource="{Binding Path=Items}" // This is your ObservableCollection
          SelectedIndex="{Binding SelectedIndexStory}">
          <e:Interaction.Triggers>
              <e:EventTrigger EventName="CellEditEnding">
                 <cmd:EventToCommand PassEventArgsToCommand="True" Command="{Binding EditStoryCommand}"/> // Mvvm light relay command
               </e:EventTrigger>
          </e:Interaction.Triggers>
          <DataGrid.Columns>
                    <DataGridTextColumn Header="Description"
                        Binding="{Binding Name, UpdateSourceTrigger=PropertyChanged}" /> // Name is property on the object i.e Items.Name
          </DataGrid.Columns>

</DataGrid>

UpdateSourceTrigger = PropertyChanged 将在目标属性更改时立即更改属性源.

UpdateSourceTrigger = PropertyChanged will change the property source immediately whenever the target property changes.

这将允许您捕获对项目的编辑,因为将事件处理程序添加到可观察集合的更改事件不会触发集合中对象的编辑.

This will allow you to capture edits to items as adding an event handler to the observable collection changed event does not fire for edits of objects in the collection.

这篇关于WPF DataGrid CellEditEnded 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆