我正在尝试将ReactiveList
绑定到DataGrid
,我想绑定到列表的ItemChanged
事件甚至Changed
事件,以便我可以更新一个Total
变量,该变量是列表中项目的总和。
这是我的列表声明:
private ReactiveList<AllocationMatrix> _matrixItems;
public ReactiveList<AllocationMatrix> MatrixItems
{
get => _matrixItems; set => this.RaiseAndSetIfChanged(ref _matrixItems, value);
}
_matrixItems = new ReactiveList<AllocationMatrix>()
{ ChangeTrackingEnabled = true };
以下是我绑定到更改事件的方式:
this.WhenAnyObservable(x => x._matrixItems.Changed)
.Subscribe(x =>
{
Trace.WriteLine("Changed");
});
最后,我试图通过编辑单个单元格(而不是添加或删除行(来引发更改事件的数据网格。
<DataGrid x:Name="MatrixGrid"
ItemsSource="{Binding MatrixItems, UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"
...
由于某种原因,编辑单元格时不会触发Changed
和ItemChanged
事件,但是如果我添加或删除一行(这不是我需要的(,它似乎会被抛出。 任何帮助将不胜感激。
你可以试试ObservableCollection<T>
例如:
ObservableCollection<int> collection = new ObservableCollection<int>();
var observable = Observable.FromEventPattern<NotifyCollectionChangedEventArgs>(collection,"CollectionChanged");
observable.Subscribe(a => { Console.WriteLine("Collection Changed !!!!! ");Debugger.Break(); });
collection.Add(1);
让我知道这是否对您有帮助。