How to subscribe to the properties of tree child nodes? #3715
-
Excuse me, I want to know how to subscribe to the attribute changes of any child node of the tree. I don't seem to find it in the documentation.
Just like the above code structure, I hope that no matter how deep the recursive structure is, I can observe the changes in IsChecked. How to do this?
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
There's no super easy way to do this at the moment. The short version for a static list looks something like (coding via TextArea, forgive typos please!): var anyCheckedChanged = Observable.Merge(
Nodes.Select(node =>
node.WhenAnyValue(x => x.IsChecked).Skip(1).Select(checked => (node, checked)))); (we skip 1 because we don't want to fire N initial states, though maybe you might want to do that!) The problem is that you need to rebuild this Observable whenever any items in the list change. This is where DynamicData is a good solution, to create a projection of your IsChecked values. Another solution is building on our previous solution, something like: var anyCheckedChange = listItemsAddedRemovedOrReplaced.StartWith(null)
.Select(_ => {
var anyCheckedChangedForThisParticularList = Observable.Merge(
Nodes.ToArray().Select(node =>
node.WhenAnyValue(x => x.IsChecked).Skip(1).Select(checked => (node, checked))));
return anyCheckedChangedForThisParticularList;
})
.Switch(); If the list is constantly changing multiple times a second, this is a pretty inefficient solution, but for most everything else it's Fine |
Beta Was this translation helpful? Give feedback.
-
I have a Treeview example that may assist https://github.com/ChrisPulman/ReactiveUITreeviewExample |
Beta Was this translation helpful? Give feedback.
There's no super easy way to do this at the moment. The short version for a static list looks something like (coding via TextArea, forgive typos please!):
(we skip 1 because we don't want to fire N initial states, though maybe you might want to do that!)
The problem is that you need to rebuild this Observable whenever any items in the list change. This is where DynamicData is a good solution, to create a projection of your IsChecked values. Another solution is building on our previous solution, something like: