我的 wpf 应用程序中有一个树视图,它动态绑定来自我的视图模型的数据。
<TreeView Grid.Column="0"
Grid.Row="0"
ItemsSource="{Binding Main}"
HorizontalAlignment="Stretch"
Name="treeView1"
VerticalAlignment="Stretch"
Height="auto"
Width="auto"
SelectedItemChanged="treeView1_SelectedItemChanged"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Top"
Margin="0,0,5,5">
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded"
Value="{Binding IsExpanded, Mode=TwoWay}" />
<Setter Property="IsSelected"
Value="{Binding IsSelected, Mode=TwoWay}" />
<Setter Property="FontWeight"
Value="Normal" />
<Style.Triggers>
<Trigger Property="IsSelected"
Value="True">
<Setter Property="FontWeight"
Value="Bold" />
</Trigger>
</Style.Triggers>
</Style>
</TreeView.ItemContainerStyle>
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:MViewModel}"
ItemsSource="{Binding Children}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Mname}" />
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:FViewModel}"
ItemsSource="{Binding Children}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding FName}" />
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:CViewModel}"
ItemsSource="{Binding Children}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Com}" />
</StackPanel>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type local:LViewModel}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Lo}" />
</StackPanel>
</DataTemplate>
</TreeView.Resources>
</TreeView>
我需要将标题添加到整个树视图中,说“家庭”,其子项是上述树视图元素。如何将标题添加到整个 treeview.so 当我单击该标题时,它应该显示所有 Mname 等等。请帮我解决这个问题。
编辑:
我用过
<HierarchicalDataTemplate DataType="{x:Type TextBlock}" ItemsSource="{Binding Children}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="Family" />
</StackPanel>
</HierarchicalDataTemplate>
在我的 TreeviewResource 中。但我仍然无法为树分配父级。
编辑2:
<TreeView>
<TreeViewItem ItemsSource="{Binding Main}" Header="Family">
<TreeViewItemContainerStyle>
//..
</TreeViewItemContainerStyle>
<TreeView.Resources>
//..
</TreeView.Resources>
</TreeViewItem>
</TreeView>
它的显示错误。
但是当我喜欢下面的时候,
<TreeView>
<TreeViewItemContainerStyle>
//..
</TreeViewItemContainerStyle>
<TreeView.Resources>
//..
</TreeView.Resources>
<TreeViewItem ItemsSource="{Binding Main}" Header="Family"/>
</TreeView>
根项目已设置。但只显示第一组孩子。子元素的子元素不显示。