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.
+2
A:
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.
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>
Carlo
2010-07-30 00:27:15
A:
I implemented this recently, on the treeview control I just set
IsHitTestVisible="False"
and added into my app.xaml
<Style TargetType="TreeViewItem">
<Setter Property="IsExpanded" Value="True" />
</Style>
Though this removes you're ability to select any of the TreeViewItems, I just did it because I wanted the simple hierachical representation of some data I had..
Jimmy Hoffa
2010-07-30 18:31:51