Reactive Extensions / Reactive UI Watch collecting, grouping and displaying it

I have a special data set that requires storing data that is grouped and ordered. Then each line should be able to recalculate the amounts each time each element is changed.

This is the main form of the final result:

enter image description here

The structure of the object is as follows:

public class MyObject : INotifyPropertyChanged { public ObservableCollection<MySubObject> Objects { get; set; } public Decimal TotalOfAll { get; set; } /* Ommited INPC and other properties for brevity */ } public class MySubObject : INotifyPropertyChanged { public Decimal Value { get; set; } public String RowType { get; set; } /* Ommited INPC and other properties for brevity */ } 

The view must bind to MyObject, and then group the Objects property using the Type property.

Now I have already done this without using reactive extensions, but it feels hacked ... I would like to do this by converting the Objects property of MyObject to an observable, which theoretically should allow me to update the summed when the value of the MySubObject property changes.

I already have a glance side of the built things, so no problem ... she launched the RX part.

Note:

I can alternatively expose my data as follows:

 public class MyObject : INotifyPropertyChanged { public ObservableCollection<MyRowObject> Objects { get; set; } public Decimal TotalOfAll { get; set; } /* Ommited INPC and other properties for brevity */ } public class MyRowObject : INotifyPropertyChanged { public ObservableCollection<MySubObject> Objects { get; set; } public String RowType { get; set; } public Decimal RowTotal { get; set; } /* Ommited INPC and other properties for brevity */ } public class MySubObject : INotifyPropertyChanged { public Decimal Value { get; set; } /* Ommited INPC and other properties for brevity */ } 

This will take care of the grouping problem, but I still can't get it to work

+4
source share
1 answer

You can use the ReactiveUI reactive assembly, which provides a number of observables, including one when the collection changes ( Changed ), and the other when changing elements in the collection ( ItemChanged ). This can be combined with ObservableAsPropertyHelper for dependent properties.

The specified totalOfAll property totalOfAll pretty simple. Another dependent property, groupedObjects is a bit more complicated. I'm not sure if I understood your grouping requirements correctly. We hope that the code below will be the starting point - it will project an assembly of subobjects into IEnumerable anonymous objects, each of which contains a total number, a row heading, and elements. You should be able to get attached to it in your presentation.

 public class MyObject : ReactiveObject { public ReactiveCollection<MySubObject> Objects { get; set; } private ObservableAsPropertyHelper<IEnumerable<object>> groupedObjectsHelper; public IEnumerable<object> GroupedObjects { get {return groupedObjectsHelper.Value;} } private ObservableAsPropertyHelper<Decimal> totalOfAllHelper; public Decimal TotalOfAll { get {return totalOfAllHelper.Value;} } public MyObject() { var whenObjectsChange= Observable.Merge(Objects.Changed.Select(_ => Unit.Default), Objects.ItemChanged.Select(_ => Unit.Default)); totalOfAllHelper= whenObjectsChange.Select(_=>Objects.Sum(o => o.Value)) .ToProperty(this, t => t.TotalOfAll); groupedObjectsHelper= whenObjectsChange.Select(_=>Objects .GroupBy(o => o.RowType) .Select(g => new {RowType=g.Key, RowTotal=g.Sum(o => o.Value), Objects=g})) .ToProperty(this, t => t.GroupedObjects); } } 
+6
source

Source: https://habr.com/ru/post/1497098/


All Articles