我希望有两个树视图,绑定到相同的数据 并且仅在它们为每个项目呈现的标题中有所不同(绑定到数据内的不同字符串属性)。
我希望当一个treeItem在一棵树中展开时,双节点也会在另一棵树中展开,而折叠也是如此。
我考虑过在数据中添加一个代表" IsExpanded"树的属性并将Expander按钮绑定到它,这样它会影响两棵树,我只是没有想法如何实现它,因为我对WPF很新。
提前感谢您提供任何帮助。 尼克斯回答后的代码:
<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");
}
}
答案 0 :(得分:0)
在IsExpanded
课程中添加媒体资源Data
,并为ItemContainerStyle
设置TreeViews
,如下所示。
不用说Data类必须实现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}"/>