首先,既然你說你以編程方式綁定列,你需要添加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
}
不過的SelectedItem爲空的細胞。 SelectedCell的set屬性僅在SelectedRow更改時調用。不是當我輸入單元格時。 – nan
@nan看到我的編輯。 –