从数据网格更新反应列表更改单元格



我正在尝试将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}"
...

由于某种原因,编辑单元格时不会触发ChangedItemChanged事件,但是如果我添加或删除一行(这不是我需要的(,它似乎会被抛出。 任何帮助将不胜感激。

你可以试试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);

让我知道这是否对您有帮助。

最新更新