首先,既然你说你以编程方式绑定列,你需要添加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看到我的编辑。 –