簡體   English   中英

如何響應CodePlex WPF DataGrid中已更改的單元格?

[英]How to I respond to changed cells in the CodePlex WPF DataGrid?

在我看來,我已經從CodePlex實現了WPF DataGrid

<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,以便可以輸入的數據保存回數據庫中。

什么是我們可以捕捉到變化的細胞,並將它們保存到數據庫中的過程

我一直在使用CellEditEndingRowEditEnding事件,它們不符合您的需求嗎?

嘗試以其他方式處理它。 代替綁定到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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM