如何使用BasedOn属性从派生设置更改基本样式的设置

时间:2013-05-02 20:17:05

标签: wpf controltemplate

我使用ControlTemplate OrangeButton进行简单的样式,并且我创建了另一种样式OrangeButton2 BasedOn OrangeButton。我试图更改边框属性,但WPF不呈现OrangeButton2中定义的新边框样式。我也尝试了相反的方法,其中OrangeButton基于OrangeButton2,没有任何变化。知道这里发生了什么,以及如何使其正确。

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

1 个答案:

答案 0 :(得分:1)

定义BorderBrush是不够的,你必须使用它。在ControlTemplate更改

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

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

这将使用您为Style定义的值。