skjagini
skjagini

Reputation: 3217

How to change settings of a base style from derived settings using BasedOn property

I have simple style with ControlTemplate OrangeButton, and I have created another style OrangeButton2 BasedOn OrangeButton. I am trying to change the border properties, but WPF doesn't render the new border style defined in OrangeButton2. I tried the opposite also, where OrangeButton is based on OrangeButton2, with no change. Any idea what is actually happening here, and how to make it correct.

   <Style x:Key="OrangeButton" TargetType="Button" >
      <Setter Property="OverridesDefaultStyle" Value="True"/>
      <Setter Property="Margin" Value="2"/>
      <Setter Property="FontFamily" Value="Verdana"/>
      <Setter Property="FontSize" Value="11px"/>
      <Setter Property="FontWeight" Value="Bold"/>
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="Button">
            <Border Name="border"
               BorderThickness="1"
               Padding="4,2"
               BorderBrush="DarkGray"
               CornerRadius="3"
               Background="{TemplateBinding Background}">
              <Grid >
                <ContentPresenter HorizontalAlignment="Center"
                                  VerticalAlignment="Center" Name="contentShadow" Style="{StaticResource ShadowStyle}">
                </ContentPresenter>
                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Name="content"/>
              </Grid>
            </Border>                
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>

    <Style x:Key="OrangeButton2" TargetType="Button" BasedOn="{StaticResource OrangeButton}">
      <Setter Property="BorderBrush" Value="Red"></Setter>
      <Setter Property="BorderThickness" Value="1"></Setter>
    </Style>  
  </Window.Resources>

  <StackPanel HorizontalAlignment="Center">
    <Button Style="{StaticResource OrangeButton2}">Hello</Button>    
  </StackPanel>

Upvotes: 0

Views: 125

Answers (1)

MatthiasG
MatthiasG

Reputation: 4532

It's not enough to define a BorderBrush, you will have to use it. In your ControlTemplate change

<Border Name="border"
    BorderThickness="1"
    Padding="4,2"
    BorderBrush="DarkGray"
    CornerRadius="3"
    Background="{TemplateBinding Background}">

to

<Border Name="border"
    BorderThickness="{TemplateBinding BorderThickness}"
    Padding="4,2"
    BorderBrush="{TemplateBinding BorderBrush}"
    CornerRadius="3"
    Background="{TemplateBinding Background}">

This will use the value you define for the Style.

Upvotes: 1

Related Questions