Question

Is there a way to reuse the simple expand [+] and collapse [-] buttons that appear next to nodes in a WPF TreeView? I would like to have a similar graphic elsewhere in my application for expanding and collapsing some controls.

Was it helpful?

Solution

It's actually a ToggleButton, I checked the TreeView template on the SimpleStyles project and this is what I found:

    <ControlTemplate TargetType="ToggleButton">
      <Grid
        Width="15"
        Height="13"
        Background="Transparent">
        <Path x:Name="ExpandPath"
          HorizontalAlignment="Left" 
          VerticalAlignment="Center" 
          Margin="1,1,1,1"
          Fill="{StaticResource GlyphBrush}"
          Data="M 4 0 L 8 4 L 4 8 Z"/>
      </Grid>
      <ControlTemplate.Triggers>
        <Trigger Property="IsChecked"
             Value="True">
          <Setter Property="Data"
              TargetName="ExpandPath"
              Value="M 0 4 L 8 4 L 4 8 Z"/>
        </Trigger>
      </ControlTemplate.Triggers>
    </ControlTemplate>

So this is what you need to do to make it work:

<Window x:Class="StackOverflowTests.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" x:Name="window1" Height="300" Width="300"
 Loaded="window1_Loaded"
 xmlns:local="clr-namespace:StackOverflowTests">
 <Window.Resources>
  <SolidColorBrush x:Key="GlyphBrush" Color="#444" />
  <ControlTemplate x:Key="toggleButtonTemplate" TargetType="ToggleButton">
   <Grid
            Width="15"
            Height="13"
            Background="Transparent">
    <Path x:Name="ExpandPath"
              HorizontalAlignment="Left" 
              VerticalAlignment="Center" 
              Margin="1,1,1,1"
              Fill="{StaticResource GlyphBrush}"
              Data="M 4 0 L 8 4 L 4 8 Z"/>
   </Grid>
   <ControlTemplate.Triggers>
    <Trigger Property="IsChecked"
                 Value="True">
     <Setter Property="Data"
                  TargetName="ExpandPath"
                  Value="M 0 4 L 8 4 L 4 8 Z"/>
    </Trigger>
   </ControlTemplate.Triggers>
  </ControlTemplate>
  <Style x:Key="toggleButtonStyle" TargetType="ToggleButton">
   <Setter Property="Template" Value="{StaticResource toggleButtonTemplate}" />
  </Style>
 </Window.Resources>
 <StackPanel>
  <ToggleButton x:Name="toggleButton" Height="20" Width="20" Style="{StaticResource toggleButtonStyle}" />
 </StackPanel>
</Window>
  • First you take the template (toggleButtonTemplate) and put it in your resources
  • Then you make a style (toggleButtonStyle) that sets the template (toggleButtonTemplate) of the control
  • Finally you tell your ToggleButton its style is toggleButtonStyle

If you just copy paste from to it should straight out work.

It's a simple process but it can give you a headache if you're not used to using templates, let me know if you have any questions.

To learn a little about the paths mini-language:

Geometry mini-language

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top