我有一个IList类型列表,称为组,每个组都有一个名为IList的子类列表。 Group和Item都有名为Name的属性。要显示此层次结构,我正在使用TreeView。
我希望将层次结构中的每个组显示为Expander,但它不会按我的意图显示。
到目前为止我的xaml:
<DataTemplate x:Key="NavItemTemplate">
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
<HierarchicalDataTemplate x:Key="NavGroupTemplate" ItemsSource="{Binding Items}" ItemTemplate="{StaticResource NavItemTemplate}">
<TextBlock Text="{Binding Name}"/>
</HierarchicalDataTemplate>
<Style x:Key="RootItemStyle" TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TreeViewItem">
<Grid>
<Expander x:Name="Exp" TextElement.FontWeight="Bold" IsExpanded="{TemplateBinding TreeViewItem.IsExpanded}">
<Expander.Header>
<ContentPresenter x:Name="ExpCP" ContentSource="Header"/>
</Expander.Header>
<ItemsPresenter/>
</Expander>
<Border x:Name="CP" Padding="25,0,0,0" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Visibility="Collapsed">
<ContentPresenter ContentSource="Header" HorizontalAlignment="Left" VerticalAlignment="Center" />
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="HasItems" Value="false">
<Setter TargetName="Exp" Property="Visibility" Value="Collapsed"/>
<Setter TargetName="CP" Property="Visibility" Value="Visible" />
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Red"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<TreeView ItemsSource="{Binding}" ItemTemplate="{StaticResource NavGroupTemplate}" ItemContainerStyle="{StaticResource RootItemStyle}">
以上风格
适用于:
Group1(扩展器)
Group2(扩展器)
...
但不适用于(目标):
Group1(扩展器)
的Item1
Group2(扩展器)
...
此外,上述款式还需要进一步修正。当我选择扩展器边界时,它会触发一个selectitem事件,应该阻止它。
答案 0 :(得分:0)
如果您未在HierarchicalDataTemplate中指定ItemTemplate(添加左边距以实际查看树层次结构),则此方法有效:
<Grid>
<Grid.Resources>
<HierarchicalDataTemplate x:Key="NavGroupTemplate" ItemsSource="{Binding Items}">
<TextBlock Text="{Binding Name}"/>
</HierarchicalDataTemplate>
<Style x:Key="RootItemStyle" TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TreeViewItem">
<Grid Margin="16 0 0 0">
<Expander x:Name="Exp" TextElement.FontWeight="Bold" IsExpanded="{TemplateBinding TreeViewItem.IsExpanded}">
<Expander.Header>
<ContentPresenter x:Name="ExpCP" ContentSource="Header"/>
</Expander.Header>
<ItemsPresenter/>
</Expander>
<Border x:Name="CP" Padding="25,0,0,0" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Visibility="Collapsed">
<ContentPresenter ContentSource="Header" HorizontalAlignment="Left" VerticalAlignment="Center" />
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="HasItems" Value="false">
<Setter TargetName="Exp" Property="Visibility" Value="Collapsed"/>
<Setter TargetName="CP" Property="Visibility" Value="Visible" />
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Red"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
<TreeView ItemsSource="{Binding}" ItemTemplate="{StaticResource NavGroupTemplate}" ItemContainerStyle="{StaticResource RootItemStyle}">
</TreeView>
</Grid>
答案 1 :(得分:0)
我之前做过类似的事情,但那是一个XmlDataProvider(不是你的IList场景)。希望它有点帮助。
<XmlDataProvider x:Key="MenuDataProvider" XPath="*">
<x:XData>
<MainMenu xmlns="">
<Admin>
<Facility>
<Test/>
</Facility>
<Carpark />
</Admin>
<Sequence>
<Lcd />
</Sequence>
</MainMenu>
</x:XData>
</XmlDataProvider>
<HierarchicalDataTemplate x:Key="NodeTemplate">
<TextBlock x:Name="tb" />
<HierarchicalDataTemplate.ItemsSource>
<Binding XPath="child::node()" />
</HierarchicalDataTemplate.ItemsSource>
<HierarchicalDataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=NodeType}" Value="Text">
<Setter TargetName="tb" Property="Text" Value="{Binding Path=Value}"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding Path=NodeType}" Value="Element">
<Setter TargetName="tb" Property="Text" Value="{Binding Path=Name}"></Setter>
</DataTrigger>
</HierarchicalDataTemplate.Triggers>
</HierarchicalDataTemplate>
看起来像:
答案 2 :(得分:0)
您的代码对我来说很好。我刚刚删除了不必要的代码,添加了子元素的边距并更改为仅粗体的组元素。
<HierarchicalDataTemplate x:Key="NavGroupTemplate" ItemsSource="{Binding Items}">
<TextBlock Text="{Binding Name}" />
</HierarchicalDataTemplate>
<Style x:Key="RootItemStyle" TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TreeViewItem">
<Grid>
<Expander x:Name="Exp" IsExpanded="{TemplateBinding TreeViewItem.IsExpanded}">
<Expander.Header>
<ContentPresenter x:Name="ExpCP" ContentSource="Header" TextElement.FontWeight="Bold" />
</Expander.Header>
<ItemsPresenter />
</Expander>
<Border x:Name="CP" Padding="35,0,0,0" Background="{TemplateBinding Background}" Visibility="Collapsed">
<ContentPresenter ContentSource="Header" HorizontalAlignment="Left" VerticalAlignment="Center" />
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="HasItems" Value="false">
<Setter TargetName="Exp" Property="Visibility" Value="Collapsed"/>
<Setter TargetName="CP" Property="Visibility" Value="Visible" />
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Red"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<TreeView ItemsSource="{Binding}" ItemTemplate="{StaticResource NavGroupTemplate}" ItemContainerStyle="{StaticResource RootItemStyle}">
</TreeView>
你能详细描述一下你的问题,所以我可以有更多想法吗?