2015-07-21 120 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 
}