2009-05-28 45 views
1

在我看來,我已經實現了一個在WPF DataGrid from CodePlex如何響應CodePlex WPF DataGrid中已更改的單元格?

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

它綁定到的ObservableCollection在我的視圖模型:

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

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

當我在網格中更改數據,它的變化,但我找不到任何事件,我可以處理這些變化,例如DataGridCellChanged,這樣我可以保存輸入數據庫的數據

什麼是過程我們可以通過它捕獲對單元格的更改並將它們保存回數據庫?

回答

0

我一直在使用事件CellEditEndingRowEditEnding,它們不適合你的需要嗎?

0

嘗試以不同的方式接近它。而不是綁定到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 
} 
相關問題