Question

I have this base style for a 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"  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>

So, in a Grid I have a rectangle. In order to have my application skinnable, I put in another resource dictionary a style based on this:

<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>

What I want, is to change the fill of the rectangle . It doesn't work. How can I do this?

Was it helpful?

Solution

Bind the Fill with Background property of the 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>

Set the Background from your derived Style,

<Style x:Key="ButtonStyle" BasedOn="{StaticResource ButtonStyle_base}">
        <Setter Property="Background" >
            <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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top