c# - wpf how to binding one TreeView to another TreeView
问题描述
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:
解决方案
我预计
t2
将具有相同数量的节点t1
。但结果是,所有的节点t1
都被删除了。为什么?
因为视觉元素的实例,例如 aTreeViewItem
只能在视觉树中出现一次。一个元素只能有一个可视父级,在这种情况下,项目将从第一个中删除TreeView
并添加到第二个中。
因此,您需要克隆TreeViewItem
您希望能够在两者中显示的每个元素TreeViews
。
推荐阅读
- javascript - 在 Javascript 中为图像描述创建鼠标悬停效果
- html - 如何设置与父元素相同的高度
- java - 为什么 println() 方法被执行两次而不是一次?
- javascript - 那里的T是什么意思
($winstonLoggerConfig ) => $winstonLogger 在温斯顿 v3 中? - c - 有人可以解释我在 C 中右移的行为吗?
- python - 需要帮助来编写正确的解析选项
- ios - 如何使用 Swift 在半横幅视图上创建 UITableview 第一个索引单元格显示?
- java - Java中TreeSet的天花板和地板功能的内部工作
- mysql - 错误:无法在只读事务中执行语句
- r - 匹配/替换对应于R中不同数据框的多个数据框列