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

查看:488
本文介绍了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> ,其中 Item 是一个类,是从WCF混合端点自动生成的。

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.

UPDATE

我尝试了CollectionChanged事件,但当 Item 被修改时,它不会被触发。

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

推荐答案

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

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天全站免登陆