c# - WPF DataGrid CellEditEnded 事件

标签 c# wpf wcf data-binding datagrid

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

我的数据网格绑定(bind)到 ObservableCollection<Item> , 其中Item是一个类,从 WCF mex 端点自动生成。

了解每次用户对集合所做的更改的最佳方式是什么。

更新

我试过 CollectionChanged 事件,结束时它不会在 Item 时触发被修改。

最佳答案

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

见下文

<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 将在目标属性更改时立即更改属性源。

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

关于c# - WPF DataGrid CellEditEnded 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10388466/

相关文章:

c# - 启用 scrollviewer 滚动每个项目而不是默认行为

wcf - 订阅 WCF 服务中的事件

c# - 有没有关于 Lucene.NET 的书籍

wpf - 除非单击鼠标,否则按钮不会重新启用的奇怪问题

c# - 如何检查重复键并从字典中删除以前的值?

.net - 如何创建 ListView 的 SelectedItem 属性的双向链接?

c# - 无法在 IIS 托管的其余 WCF 服务中加载第三方 dll

c# - 如何将 .NET 日期时间转换为 JSON

c# - 当使用的值可以隐式转换为这些结构时,为什么我不能使用采用两个结构的重载 ==?

c# - 从 app.config c# 中读取所有键值