It could be solved by a DependencyProperty but following the MVVM path is recommended as the whole nature of these XAML stuff (WPF, MAUI, Avalonia, ...) is MVVM.
Starting with a ViewModel for the Window with two properties for the nodes and the current selected node. (Used the nuget package CommunityToolkit.MVVM)
public partial class MainApplicationWindowModel : ObservableObject
{
public ObservableCollection<OutlineItem> Nodes { get; } =
[
new OutlineItem( "Animals", "animals are nice",
[
new OutlineItem( "Mammals", "mammals are nice",
[
new OutlineItem( "Lion", "Lions are nice" ),
new OutlineItem( "Cat", "Cats are nice" ),
new OutlineItem( "Zebra", "Zebras are nice" )
] )
] )
];
[ObservableProperty] public partial OutlineItem? CurrentNode { get; set; }
}
The window needs some modifications
<!-- setting the data type -->
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:prototype="clr-namespace:Vault3.Desktop.Avalonia.Prototype"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:DataType="prototype:MainApplicationWindowModel"
x:Class="Vault3.Desktop.Avalonia.Prototype.MainApplicationWindow"
Title="Vault 3"
>
<!-- setting the data context -->
<Window.DataContext>
<prototype:MainApplicationWindowModel/>
</Window.DataContext>
<!-- [...] -->
<!-- binding the treeview to the vm properties -->
<TreeView
Name="Treeview"
SelectedItem="{Binding CurrentNode}"
ItemsSource="{Binding Nodes}"
<!-- [...] -->
<!-- binding the text box with the current node -->
<TextBox
Name="Text"
Text="{Binding CurrentNode.Text}"
<!-- [...] -->
This will push the edits in the text box back to the selected node. But you might want the OutlineItem also observable.
public partial class OutlineItem : ObservableObject
{
public ObservableCollection<OutlineItem>? SubNodes { get; }
[ObservableProperty] public partial string Title { get; set; }
[ObservableProperty] public partial string Text { get; set; }
public OutlineItem(string title, string text)
{
Title = title;
Text = text;
}
public OutlineItem(string title, string text, ObservableCollection<OutlineItem> subNodes)
{
Title = title;
Text = text;
SubNodes = subNodes;
}
}