问题描述
第 2 天,我在网上搜索,但没有找到解决方案.以这样的元素为例:
For the 2nd day I'm scouring the web and have not found a solution.Take an element like this:
<TreeView ItemsSource="{Binding Types}" Width="300">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type models:Type}"
ItemsSource="{Binding SubTypes}">
<TextBlock Text="{Binding Name}"/>
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate DataType="{x:Type SubType}">
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
我将 Material NuGet 库用于基本样式.但是现在我需要在第一级项目上禁用悬停、选择等,并且只允许对子项目进行选择/悬停.
I use the Material NuGet library for base styles. Now however I need to disable the hover, select, etc. on the first level items and only allow the selection/hover for the subitems.
但我似乎发现的一切都是关于为每个项目的内容设置样式或全局设置所有内容的样式.
But everything I seem to find is about styling the contents of each item or styling everything globally.
A <- remove selection/hover (pref single click too but that's another topic)
A1 <- maintain original style, hover and select
A2 <- maintain original style, hover and select
A3 <- maintain original style, hover and select
B <- remove selection/hover (pref single click too but that's another topic)
B1 <- maintain original style, hover and select
B2 <- maintain original style, hover and select
B3 <- maintain original style, hover and select
推荐答案
听起来您并不真的希望每个顶级项目都像一个普通的 TreeViewItem
.在这种情况下,为什么不将顶级项移到 TreeView
之外?
Sounds like you don't really want each top-level item to act like a normal TreeViewItem
. In that case, why not move the top-level items outside of the TreeView
?
基本上,您将拥有顶级项目的 ItemsControl
,其中项目模板的作用有点像包含 TreeView
的 Expander
代码>它下面的项目.您可以根据自己的喜好设置顶级项目的样式.
Basically, you'd have an ItemsControl
of the top-level items, where the item template acts a bit like an Expander
containing a TreeView
of the items underneath it. You could style the top-level items to look however you like.
缺点是顶级项目下的树将单独虚拟化,而不是整体虚拟化.也就是说,他们不会共享容器.除非您拥有吨顶级项目,否则这可能没什么大不了的.
The downside would be that the trees under the top-level items would be virtualized individually, not as a whole. That is, they would not share containers. Unless you have a ton of top-level items, that probably won't be a big deal.
示例:
<ItemsControl xmlns:s="clr-namespace:System;assembly=mscorlib"
ItemsSource="{Binding Types}">
<ItemsControl.Resources>
<ControlTemplate x:Key="ExpanderButtonTemplate" TargetType="ToggleButton">
<Border Background="Transparent" Padding="3,2">
<ContentPresenter />
</Border>
</ControlTemplate>
<Style TargetType="Expander">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Expander">
<DockPanel LastChildFill="True">
<ToggleButton DockPanel.Dock="Top"
IsChecked="{Binding IsExpanded, RelativeSource={RelativeSource TemplatedParent}}"
Template="{StaticResource ExpanderButtonTemplate}">
<ContentPresenter ContentSource="Header" />
</ToggleButton>
<Border>
<ContentPresenter x:Name="contentSite" Visibility="Collapsed" />
</Border>
</DockPanel>
<ControlTemplate.Triggers>
<Trigger Property="IsExpanded" Value="True">
<Setter TargetName="contentSite" Property="Visibility" Value="Visible" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ItemsControl.Resources>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Expander Header="{Binding Name}">
<TreeView ItemsSource="{Binding SubTypes}" BorderThickness="0" Padding="0">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate DataType="{x:Type models:Type}"
ItemsSource="{Binding SubTypes}">
<TextBlock Text="{Binding Name}"/>
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate DataType="{x:Type models:SubType}">
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</Expander>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
单击顶级项目之一以展开其下方的树.
Click on one of the top-level items to expand the tree beneath it.
这篇关于您如何仅设置 TreeView 中的顶级项目的样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!