有谁知道如何隐藏树视图的[ - ]按钮?树视图永远不会崩溃,因此我不需要根节点具有折叠选项。我知道我可以使用带有stye的listview来缩进[0]元素,使其像树视图根节点一样。然而,我正在使用的绑定等类型请求树视图,我不知道如何访问该按钮并禁用它。
答案 0 :(得分:5)
这应该有效。您需要修改TreeViewItem的ControlTemplate,[+] / [ - ]按钮实际上是TreeViewItem的ControlTemplate中的ToggleButton所以您需要做的就是将其可见性设置为隐藏或折叠。
创建一个名为“StackOverflowTests”的项目(这样您就不必更改任何内容)并将此代码粘贴到Window1.xaml文件中。应该开箱即用:
<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>
答案 1 :(得分:0)
我最近实施了这个。在TreeView控件上我只设置:
IsHitTestVisible="False"
并添加到我的app.xaml:
<Style TargetType="TreeViewItem">
<Setter Property="IsExpanded" Value="True" />
</Style>
虽然这样就无法选择任何TreeViewItems。 我之所以这么做是因为我想要一些数据的简单层次表示。