如何使两个树视图一起展开和折叠
本文关键字:折叠 一起 视图 何使两 | 更新日期: 2023-09-27 18:08:12
我想有两个树视图,它们被绑定到相同的数据不同之处在于它们为每个条目呈现的标题(绑定到数据中不同的字符串属性)。
我希望当treeItem在一棵树中展开时,孪生节点也会在另一棵树中展开,对于折叠也是如此。
我想在数据中添加一个属性,表示树的"IsExpanded"属性,并将Expander按钮绑定到它,这样它会影响两棵树,我只是不知道如何实现它,因为我是WPF的新手。
提前感谢任何形式的帮助。nit答案后的代码:
<Window x:Class="MyApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:model="clr-namespace:MyApp.Model"
xmlns:viewModel="clr-namespace:MyApp.ViewModels"
xmlns:views="clr-namespace:MyApp.Views"
xmlns:converters="clr-namespace:MyApp.Converters"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<converters:EnumToPicConverter x:Key="Converter"></converters:EnumToPicConverter>
<!--Control colors.-->
<Style x:Key="MyTreeViewItemStyle" TargetType="TreeViewItem">
<Setter Property="IsExpanded" Value="{Binding Path=IsExpanded}" />
</Style>
<HierarchicalDataTemplate DataType="{x:Type model:TreeNode}" ItemsSource="{Binding ChildListNodes}">
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Path=EntityType,Converter={StaticResource Converter}}" />
<TextBlock Margin="5,0" Text="{Binding Name1}" />
</StackPanel>
</HierarchicalDataTemplate>
</Window.Resources>
<Window.DataContext>
<viewModel:TreeViewModel/>
</Window.DataContext>
<DockPanel>
<Grid DockPanel.Dock="Top" Name="LoadRow">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
<views:DbChooser Path="{Binding Path1}" ReloadCommand="{Binding LoadFileACommand}" Grid.Column="0"/>
<views:DbChooser Path="{Binding Path2}" ReloadCommand="{Binding LoadFileBCommand}" Grid.Column="1"/>
</Grid>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"></ColumnDefinition>
<ColumnDefinition Width="1*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TreeView Name="LeftTree" ItemsSource="{Binding RootNode}" ItemContainerStyle="{StaticResource MyTreeViewItemStyle}" Grid.Column="0" />
<TreeView Name="RightTree" ItemsSource="{Binding RootNode}" ItemContainerStyle="{StaticResource MyTreeViewItemStyle}" Grid.Column="1" />
</Grid>
</DockPanel>
我在TreeNode c#中添加的代码:
public bool IsExpanded
{
get { return _isExpanded; }
set
{
_isExpanded = value;
RaisePropertyChangedEvent("IsExpanded");
}
}
在Data
类中添加属性IsExpanded
,并将ItemContainerStyle
设置为TreeViews
,如下所示。不必说数据类必须实现INotifyPropertyChanged
,并为IsExpanded
和其他属性引发属性更改通知
<Window.Resources>
<Style x:Key="MyTreeViewItemStyle" TargetType="TreeViewItem">
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
</Style>
<HierarchicalDataTemplate DataType="{x:Type model:TreeNode}" ItemsSource="{Binding ChildListNodes}">
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Path=EntityType,Converter={StaticResource Converter}}" />
<TextBlock Margin="5,0" Text="{Binding Name1}" />
</StackPanel>
</HierarchicalDataTemplate >
</Window.Resources>
<TreeView x:Name="Tree1" ItemContainerStyle="{StaticResource MyTreeViewItemStyle}"/>
<TreeView x:Name="Tree2" ItemContainerStyle="{StaticResource MyTreeViewItemStyle}"/>