Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Expander button style with + and -

Tags:

styles

wpf

Styling gurus, I need help coming up with an Expander style that looks like the one found in Visual Studio's code editor. So far, I have come up with this

<ControlTemplate x:Key="ExpanderToggleButton" TargetType="ToggleButton">               
                <Border
                    SnapsToDevicePixels="true"
                    Height="12" 
                    Name="Border"
                    CornerRadius="0"                   
                    Margin="2,4" 
                    Background="Transparent"                      
                    BorderBrush="Black"                       
                    BorderThickness="0.5" >                   
                <TextBlock  Name="Arrow" Text="+"  
                            Foreground="{StaticResource GlyphBrush}" 
                            HorizontalAlignment="Center"  VerticalAlignment="Center" />
            </Border>
                <ControlTemplate.Triggers>                  
                <Trigger Property="IsChecked" Value="true">                      
                    <Setter TargetName="Arrow" Property="Text" Value="-" />
                </Trigger>                 
            </ControlTemplate.Triggers>
        </ControlTemplate>

which looks like this in collapsed state.

enter image description here

It clearly needs some fine tuning, which is what I need help with. My challenge is getting the correct button content and its vertical alignment. In the attached figure, the expanders are simply being added to a dock panel, so I do not understand why the button content is not centered as specified.

TIA.

like image 321
Klaus Nji Avatar asked Oct 10 '11 14:10

Klaus Nji


1 Answers

Here is a style I use.

<Style x:Key="ExpandCollapseToggleStyle" TargetType="ToggleButton">
    <Setter Property="Focusable" Value="False"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ToggleButton">
                <Grid Width="14" Height="14">
                    <Rectangle Fill="{DynamicResource primaryBackgroundBrush}" />
                    <Border Name="ExpandBorder" RenderOptions.EdgeMode="Aliased" BorderBrush="Black" BorderThickness="2">
                        <Path RenderOptions.EdgeMode="Aliased" Name="ExpandPath" Stroke="Black" Margin="0" StrokeThickness="2" Data="M 5 1 L 5 9 M 1 5 L 9 5" />
                    </Border>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsChecked" Value="True">
                        <Setter Property="Data" TargetName="ExpandPath" Value="M 1 5 L 9 5"/>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter TargetName="ExpandBorder" Property="BorderBrush" Value="Gray" />
                        <Setter TargetName="ExpandPath" Property="Stroke" Value="Gray" />
                        <Setter Property="Data" TargetName="ExpandPath" Value=""/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
like image 192
Brady Avatar answered Nov 06 '22 05:11

Brady