WPF Design

Hey, I'm trying to style buttons in WPF. I have a basic style:

    <Style x:Key="buttonStyle" TargetType="{x:Type ButtonBase}">
    <Setter Property="FontWeight" Value="Bold"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate x:Name="ButtonBaseControlTemplate" TargetType="{x:Type ButtonBase}">
                <Grid SnapsToDevicePixels="True">
                    <Border x:Name="Bd" Background="#9BC4E2" BorderBrush="Black"
                            BorderThickness="3,3,3,3" CornerRadius="7,7,7,7" Padding="5,5,5,5">
                        <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" x:Name="Content"
                                      ContentSource="Content"
                                      RecognizesAccessKey="True" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    </Border>
                </Grid>
             <-- Removed triggers here -->
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

But I need to also use this for the toggle buttons, just to change the background color of IsChecked. But I can’t access the border to set the background when I try like this:

<Style TargetType="{x:Type Button}" BasedOn="{StaticResource buttonStyle}">
</Style>

<Style TargetType="{x:Type ToggleButton}" BasedOn="{StaticResource buttonStyle}">
    <Style.Triggers>
        <Trigger Property="IsChecked" Value="True">
            <Setter Property="Background"  Value="White"/> <-- Need to specify target name of the border here somehow -->
        </Trigger>
    </Style.Triggers>
</Style>

Any suggestions? that

+3
source share
1 answer

This is pretty much what TemplateBinding is for

http://devlicio.us/blogs/christopher_bennage/archive/2008/07/04/templatebinding-a-bridge-between-styles-and-templates.aspx

Change the property Border.Backgroundto

Background="{TemplateBinding Background}"

and add this setter to the buttonStyle style

<Setter Property="Background" Value="#9BC4E2" />

Then you can declare your ToggleButtonas follows

<ToggleButton Content="ToggleButton" Background="Green" />
+9
source

All Articles