filter taking into account value of previous elements: (how) can it be done? #927
-
I'm trying to do something as follows (and I have no idea if this is possible with dynamic data, or how to approach it) I have a SourceCache that contains a list of objects like the following (contrived example):
Looking at the above list, the entry with name "c" and value:"3" is not valid, because the entry with name :"b" does not contain value:"1". The entry with name:"c" and value:"4" on the other hand is valid, because the entry with name:"b" contains value "2". The "isparent" flag indicates that another entry down the list depends on this entry's value. Since the entry with name "b" has value "2", I would like to filter the list so that I keep only
I think I need a way to filter the SourceCache while accumulating some state/context information (i.e. record the value of each element marked as isparent:true, so that the filter, when it arrives at item c can lookup in its accumulated context information so far what the value of item "b" was to decide if it entry for item c is valid or not. These lists are converted into ui controls using some DataTemplate, and the above is an attempt at generating some generic ui which dynamically can display/hide different controls based on the value of some parent control. (Note: Instead of actually filtering, an alternative could be to add a visible flag into the objects, but in order to correctly manipulate those flags, I again somehow need access to some accumulating state). Is something like this possible? Or is something completely different needed? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
You want using var source = new SourceCache<long, Item>()
{
new Item()
{
Id = 1,
Value = 1
},
new Item()
{
Id = 2,
Value = 2
},
new Item()
{
Id = 3,
Value = 3,
ParentId = 2,
ParentValue = 1
},
new Item()
{
Id = 4,
Value = 4,
ParentId = 2,
ParentValue 2
}
};
using var subscription = source
.Connect()
.FilterOnObservable(item => (item.ParentId is long parentId)
? source
.Watch(parentId)
.Select(parentChange => parentChange.Current.Value == item.ParentValue)
: Observable.Return(true))
.Subscribe(); If you don't have direct access to the |
Beta Was this translation helpful? Give feedback.
You want
.FilterOnObservable()
. Something along the lines of...