问题描述
有谁知道如何隐藏[ - ]按钮一个TreeView?树形视图将永远不会坍塌,因此我不需要根节点有崩溃的选择。我知道我可以使用一个ListView与缩进[0]元素表现得像一个TreeView根节点麦粒肿。不过我使用等结合那种要求一个TreeView,我不知道如何访问按钮,并禁用它。
Does anyone know how to hide the [-] button for a treeview? The treeview will never be collapsed, and therefore I dont need the root node to have the collapse option. I know I could use a listview with a stye that indents the [0] element to act like a treeview root node. However the binding I'm using etc. kind of asks for a treeview, and I'm not sure how to access the button and disable it.
推荐答案
这应该工作。您需要修改树型视图,的控件模板的[+] / [ - ]按钮实际上是TreeViewItem的控件模板内的切换按钮,所有您需要做的是设置其能见度隐藏或折叠
This should work. You need to modify the ControlTemplate of the TreeViewItem, the [+] / [-] button is actually a ToggleButton inside the TreeViewItem's ControlTemplate so all you need to do is set its visibility to hidden or collapsed.
创建一个名为StackOverflowTests项目(所以你没有改变任何东西),并粘贴此code在Window1.xaml文件。应工作的权利开箱:
Create a project called "StackOverflowTests" (so you don't have to change anything) and paste this code in your Window1.xaml file. Should work right out of the box:
<ControlTemplate x:Key="invisibleButtonTreeViewItemTemplate" TargetType="TreeViewItem" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" MinWidth="19" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<!-- Make the ToggleButton invisible -->
<ToggleButton IsChecked="False" Visibility="Hidden" ClickMode="Press" Name="Expander" />
<Border BorderThickness="{TemplateBinding Border.BorderThickness}" Padding="{TemplateBinding Control.Padding}" BorderBrush="{TemplateBinding Border.BorderBrush}" Background="{TemplateBinding Panel.Background}" Name="Bd" SnapsToDevicePixels="True" Grid.Column="1">
<ContentPresenter Content="{TemplateBinding HeaderedContentControl.Header}" ContentTemplate="{TemplateBinding HeaderedContentControl.HeaderTemplate}" ContentStringFormat="{TemplateBinding HeaderedItemsControl.HeaderStringFormat}" ContentSource="Header" Name="PART_Header" HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
</Border>
<ItemsPresenter Name="ItemsHost" Grid.Column="1" Grid.Row="1" Grid.ColumnSpan="2" />
</Grid>
<ControlTemplate.Triggers>
<!-- Remove this trigger so it does not collapse on double click or-->
<!--<Trigger Property="TreeViewItem.IsExpanded">
<Setter Property="UIElement.Visibility" TargetName="ItemsHost">
<Setter.Value>
<x:Static Member="Visibility.Collapsed" />
</Setter.Value>
</Setter>
<Trigger.Value>
<s:Boolean>False</s:Boolean>
</Trigger.Value>
</Trigger>-->
<Trigger Property="ItemsControl.HasItems">
<Setter Property="UIElement.Visibility" TargetName="Expander">
<Setter.Value>
<x:Static Member="Visibility.Hidden" />
</Setter.Value>
</Setter>
<Trigger.Value>
<s:Boolean>False</s:Boolean>
</Trigger.Value>
</Trigger>
<Trigger Property="TreeViewItem.IsSelected">
<Setter Property="Panel.Background" TargetName="Bd">
<Setter.Value>
<DynamicResource ResourceKey="{x:Static SystemColors.HighlightBrushKey}" />
</Setter.Value>
</Setter>
<Setter Property="TextElement.Foreground">
<Setter.Value>
<DynamicResource ResourceKey="{x:Static SystemColors.HighlightTextBrushKey}" />
</Setter.Value>
</Setter>
<Trigger.Value>
<s:Boolean>True</s:Boolean>
</Trigger.Value>
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="TreeViewItem.IsSelected">
<Condition.Value>
<s:Boolean>True</s:Boolean>
</Condition.Value>
</Condition>
<Condition Property="Selector.IsSelectionActive">
<Condition.Value>
<s:Boolean>False</s:Boolean>
</Condition.Value>
</Condition>
</MultiTrigger.Conditions>
<Setter Property="Panel.Background" TargetName="Bd">
<Setter.Value>
<DynamicResource ResourceKey="{x:Static SystemColors.ControlBrushKey}" />
</Setter.Value>
</Setter>
<Setter Property="TextElement.Foreground">
<Setter.Value>
<DynamicResource ResourceKey="{x:Static SystemColors.ControlTextBrushKey}" />
</Setter.Value>
</Setter>
</MultiTrigger>
<Trigger Property="UIElement.IsEnabled">
<Setter Property="TextElement.Foreground">
<Setter.Value>
<DynamicResource ResourceKey="{x:Static SystemColors.GrayTextBrushKey}" />
</Setter.Value>
</Setter>
<Trigger.Value>
<s:Boolean>False</s:Boolean>
</Trigger.Value>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="Template" Value="{StaticResource invisibleButtonTreeViewItemTemplate}" />
</Style>
</Window.Resources>
<TreeView>
<TreeViewItem Header="Item 1" IsExpanded="True">
<TreeViewItem Header="Item 1.1" IsExpanded="True" />
<TreeViewItem Header="Item 1.2" IsExpanded="True">
<TreeViewItem Header="Item 1.2.1" IsExpanded="True" />
</TreeViewItem>
</TreeViewItem>
<TreeViewItem Header="Item 2" IsExpanded="True">
<TreeViewItem Header="Item 2.1" IsExpanded="True" />
</TreeViewItem>
</TreeView>
这篇关于树形隐藏[+] [ - ]按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!