views:

697

answers:

1

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.

+4  A: 

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

Carlo
Fantastic answer, thank you!
emddudley
Is there any way to use the system default graphic for the operating system? For example under Windows XP it is the [+] whereas under Vista it is a triangle.
emddudley
You can always change the Data property of the "ExpandPath" Path (both in the default one and the Trigger). I'm talking about the strings with the "M 0 4 L..." codes (called geometry mini-language, I added a link at the end of my answer). You'd have to learn about paths and the mini-language though, which I haven't played much with. But try modifying the values, and you'll see how the figures for expanded and not expanded change.
Carlo