I'm trying to bind TreeView t2
to TreeView t1
in xaml as below:
<Window x:Class="WpfApp2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
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:local="clr-namespace:WpfApp2"
mc:Ignorable="d"
Title="MainWindow" Height="620" Width="600">
<Window.Resources>
<HierarchicalDataTemplate DataType="{x:Type TreeViewItem}" ItemsSource="{Binding Items}">
<StackPanel Orientation="Horizontal" >
<TextBlock Background="AliceBlue" Text="{Binding Path=Header, Mode=TwoWay}" Width="220"/>
</StackPanel>
</HierarchicalDataTemplate>
</Window.Resources>
<StackPanel>
<TreeView x:Name="t1">
<TreeView.Items>
<TreeViewItem Header="a"></TreeViewItem>
<TreeViewItem Header="b"></TreeViewItem>
</TreeView.Items>
</TreeView>
<TreeView x:Name="t2" ItemsSource="{Binding Items, ElementName=t1}"></TreeView>
</StackPanel>
I expected t2
will have the same number of nodes with t1
. But the result is that, all nodes of t1
are deleted. Why
?
I expected result:
The actual result:
TreeViewItems
one to one to second, replacing parent... I think.