建立在Q&A在这里:WPF C#-将选项卡选择绑定到树查看选择
是否有可能将treeViewItem
及其所有children
和潜在的未来children
绑定到相同的tabItem
选择?
<TreeViewItem Header="Letters" x:Name="tab1">
<TreeViewItem Header="a" x:Name="tab1_1"/>
<TreeViewItem Header="b" x:Name="tab1_2"/>
<TreeViewItem Header="c" x:Name="tab1_3"/>
<TreeViewItem Header="d" x:Name="tab1_4"/>
<TreeViewItem Header="e" x:Name="tab1_5"/>
<TreeViewItem Header="f" x:Name="tab1_6"/>
</TreeViewItem>
目前,当点击带有以下代码的子节点时,它将取消选择选项卡:
<TabItem x:Name="tabLetters"
IsSelected="{Binding ElementName=tab1, Path=IsSelected}">
</TabItem>
使用SelectedValue和SelectedValuePath。
TreeViewItem子项和父项必须具有相同的Uid。
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel>
<TreeView x:Name="treeView" Height="200" SelectedValuePath="Uid">
<TreeViewItem Header="Letters-1" x:Name="tab1" Uid="Letters-1">
<TreeViewItem Header="a" Uid="Letters-1"/>
<TreeViewItem Header="b" Uid="Letters-1"/>
<TreeViewItem Header="c" Uid="Letters-1"/>
</TreeViewItem>
<TreeViewItem Header="Letters-2" x:Name="tab2" Uid="Letters-2">
<TreeViewItem Header="d" Uid="Letters-2"/>
<TreeViewItem Header="e" Uid="Letters-2"/>
<TreeViewItem Header="f" Uid="Letters-2"/>
</TreeViewItem>
<TreeViewItem Header="Letters-3" x:Name="tab3" Uid="Letters-3">
<TreeViewItem Header="g" Uid="Letters-3"/>
<TreeViewItem Header="h" Uid="Letters-3"/>
<TreeViewItem Header="i" Uid="Letters-3"/>
</TreeViewItem>
</TreeView>
<TabControl x:Name="tabControl" Height="100"
SelectedValuePath="Header"
SelectedValue="{Binding ElementName=treeView, Path=SelectedValue, Mode=OneWay}">
<TabItem Header="Letters-1"/>
<TabItem Header="Letters-2"/>
<TabItem Header="Letters-3"/>
</TabControl>
</StackPanel>
</Window>