2015-07-21 200 views
2

我有一個DataGrid綁定到DataRowViewObservableCollection。在我開始輸入之前調用DataGrid.BeginningEdit。在焦點丟失後調用CellEditEnding。我需要一個事件,只要我輸入一個單元格就會觸發。我該怎麼辦?WPF中DataGrid的CellValueChanged事件?

private static ObservableCollection<DataRowView> _dataGridSrcCollection = new ObservableCollection<DataRowView>(); 
public static ObservableCollection<DataRowView> DataGridSrcCollection 
{ 
    get 
    { 
    return _dataGridSrcCollection; 
    } 
    set 
    { 
    if (value != _dataGridSrcCollection) 
    { 
     _dataGridSrcCollection = value;   
    } 
    } 
} 

我是以編程方式綁定每列。

回答

0

我懷疑有任何CellValueChanged事件DataGrid但是假設你所有的數據網格列文本列,那麼你可以使用TextChanged事件如下:

的XAML:

<DataGrid Grid.Row="0" 
       ItemsSource="{Binding DataGridSrcCollection}" 
       SelectionUnit="Cell" 
       SelectionMode="Single" > 
     <DataGrid.Columns> 
      <DataGridTextColumn Header="your header" Binding="{Binding Path=YourProperty}" > 
       <DataGridTextColumn.EditingElementStyle> 
        <Style TargetType="{x:Type TextBox}"> 
         <EventSetter Event="TextChanged" Handler="CellValueChanged" /> 
        </Style> 
       </DataGridTextColumn.EditingElementStyle> 
      </DataGridTextColumn> 
     </DataGrid.Columns> 
    </DataGrid> 

後面的代碼:

private void CellValueChanged(object sender, TextChangedEventArgs e) 
    { 
     // your code 
    } 
+0

不過的SelectedItem爲空的細胞。 SelectedCell的set屬性僅在SelectedRow更改時調用。不是當我輸入單元格時。 – nan

+0

@nan看到我的編輯。 –

0

首先,既然你說你以編程方式綁定列,你需要添加Binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged給那些綁定。

然後,您需要訂閱集合中每個DataRowView對象的PropertyChanged事件。要做到這一點,以避免鬆動的事件處理程序,最好的辦法是在CollectionChanged事件您的ObservableCollection的,就像這樣:

private static ObservableCollection<DataRowView> _dataGridSrcCollection = new ObservableCollection<DataRowView>(); 
public static ObservableCollection<DataRowView> DataGridSrcCollection 
{ 
    get 
    { 
     return _dataGridSrcCollection; 
    } 
    set 
    { 
     if (value != _dataGridSrcCollection) 
     { 
      if (_dataGridScrCollection != null) 
      { 
       _dataGridScrCollection.CollectionChanged -= DataGridScrCollection_CollectionChanged; 

       foreach (var row in _dataGridScrCollection) 
        row.PropertyChanged -= DataRowView_PropertyChanged; 
      } 

      if (value != null) 
      { 
       value.CollectionChanged += DataGridScrCollection_CollectionChanged; 

       foreach (var row in value) 
        row.PropertyChanged += DataRowView_PropertyChanged; 
      } 

      _dataGridSrcCollection = value;   
     } 
    } 
} 

private void DataGridScrCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) 
{ 
    if (e.OldItems != null) 
     foreach (var item in e.OldItems) 
      ((DataRowView)item).PropertyChanged -= DataRowView_PropertyChanged; 

    if (e.NewItems != null) 
     foreach (var item in e.NewItems) 
      ((DataRowView)item).PropertyChanged += DataRowView_PropertyChanged; 
} 

private void DataRowView_PropertyChanged(object sender, PropertyChangedEventArgs e) 
{ 
    // This will be called every time a change is made to any cell 
}