通过基于样式属性覆盖样式

时间:2014-03-26 12:42:41

标签: c# wpf resources resourcedictionary

我有一个按钮的基本样式:

<Style x:Key="ButtonStyle_base" TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid Name="grid" Margin="0,0,0,0">
                        <Rectangle Name="rectangle" HorizontalAlignment="Stretch"  VerticalAlignment="Stretch" >
                            <Rectangle.Effect>
                                <DropShadowEffect BlurRadius="3" Opacity="0.4" ShadowDepth="6"/>
                            </Rectangle.Effect>
                        </Rectangle>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsFocused" Value="True" />
                        <Trigger Property="IsDefaulted" Value="True" />
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
</Style>

所以,在网格中我有一个矩形。为了让我的应用程序可换肤,我在另一个资源字典中添加了一个基于此的样式:

<Style x:Key="ButtonStyle" BasedOn="{StaticResource ButtonStyle_base}">
        <Setter Property="Rectangle.Fill" >
            <Setter.Value>
                <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1" >
                    <GradientStop Color="#FFFFFFFF" Offset="0.5" />
                    <GradientStop Color="#CCFFFFDD" Offset="1" />
                </LinearGradientBrush>
            </Setter.Value>
        </Setter>
    </Style>

我想要的是改变矩形的填充。它不起作用。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:4)

将填充与Background的{​​{1}}属性绑定,

Button

从派生的样式

设置背景
<Style x:Key="ButtonStyle_base" TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid Name="grid" Margin="0,0,0,0">
                        <Rectangle Name="rectangle" HorizontalAlignment="Stretch"  Fill="{TemplateBinding Background}" VerticalAlignment="Stretch" >
                            <Rectangle.Effect>
                                <DropShadowEffect BlurRadius="3" Opacity="0.4" ShadowDepth="6"/>
                            </Rectangle.Effect>
                        </Rectangle>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsFocused" Value="True" />
                        <Trigger Property="IsDefaulted" Value="True" />
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
</Style>