【问题标题】:How to I respond to changed cells in the CodePlex WPF DataGrid?如何响应 CodePlex WPF DataGrid 中更改的单元格?
【发布时间】:2010-11-18 18:08:43
【问题描述】:

在我看来,我已经实现了WPF DataGrid from CodePlex

<toolkit:DataGrid x:Name="CodePlexDataGrid" 
    Style="{StaticResource ToolkitDataGrid}" 
    ItemsSource="{Binding Customers}"/>

它绑定到我的 ViewModel 中的 ObservableCollection:

private ObservableCollection<Customer> _customers;
public ObservableCollection<Customer> Customers
{
    get
    {
        return _customers;
    }

    set
    {
        _customers = value;
        OnPropertyChanged("Customers");
    }
}

当我更改网格中的数据时,它会发生变化,但 我找不到可以处理的事件来捕捉这些更改,例如DataGridCellChanged 以便我可以保存输入回数据库的数据

我们可以通过什么过程捕获对单元格的更改并将它们保存回数据库?

【问题讨论】:

    标签: c# wpf datagrid


    【解决方案1】:

    我一直在使用CellEditEndingRowEditEnding 事件,它们不符合您的需要吗?

    【讨论】:

      【解决方案2】:

      尝试以不同的方式接近它。不是绑定到 DataGrid 上的事件,而是在 Customer 上实现 INotifyPropertyChanged 并处理 Customer 对象的属性更改事件。在 WPF(与 Silverlight 相对)中,我认为您可以使用 BindingList 而不是 ObservableCollection 来观察集合中任何项目的属性更改。

      对于 Silverlight,我创建了 ObservableCollection 的子类,它为添加到集合中的任何项目连接 PropertyChanged 事件处理程序,然后将它们冒泡到集合公开的 ItemPropertyChanged 事件。

      我可以这样做:

      myCollection.ItemPropertyChanged += (sender,e) => {
         // sender is the item whose property changed
         // e is PropertyChangedEventArgs which has the name of the property that changed
      }
      

      【讨论】:

        猜你喜欢
        • 2011-05-09
        • 2020-01-10
        • 2023-03-28
        • 2016-03-29
        • 2015-11-19
        • 1970-01-01
        • 2015-05-14
        • 2013-07-20
        • 1970-01-01
        相关资源
        最近更新 更多