How to observe property changes on any sublevel of a tree?
Consider for example a class TreeNode
with the properties Name
and ChildNodes
. How to observe Name
changes on any sublevel of a TreeNode
?
The usage might look something like this:
rootNode.FlattenTreeToObservable(x => x.ChildNodes)
.WhenAnyValue(x => x.Name)
.Subscribe(...)
TreeNode example:
// NuGet: Install-Package reactiveui
// In case of Splat version issue: Install-Package Splat -Version 1.6.2
using ReactiveUI;
public class TreeNode: ReactiveObject
{
public string Name
{
get { return this._name; }
set { this.RaiseAndSetIfChanged(ref this._name, value); }
}
private string _name = "";
public ReactiveList<TreeNode> ChildNodes
{
get { return this._childNodes; }
set { this.RaiseAndSetIfChanged(ref this._childNodes, value); }
}
private ReactiveList<TreeNode> _childNodes = new ReactiveList<TreeNode>();
}
I couldn't get your code to run - I ended up with a DLL versioning issue so I wrote a basic class that uses the same structure as yours to get this to work:
I then added the following method:
Now when I run this test code:
I get this output: