开发者

WPF BasedOn Styling

Hey I'm trying to style buttons in WPF. I have a base 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"
      开发者_JS百科                                RecognizesAccessKey="True" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    </Border>
                </Grid>
             <-- Removed triggers here -->
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

But I need to use this for toggle buttons too, just to change the IsChecked background colour. 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? ta


This is pretty much what TemplateBinding is designed for

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

Change the Border.Background property to be

Background="{TemplateBinding Background}"

and add this Setter to your buttonStyle Style

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

Then you can declare your ToggleButtons like this

<ToggleButton Content="ToggleButton" Background="Green" />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜