覆盖样式中的特定 属性

Overriding a specific property in style

我有一个带有切换按钮的应用程序,需要根据选中状态更改背景。此应用程序还有一些常规按钮,它们与切换按钮共享相同的背景。而且,它们都有圆角。

所以我想到了这些样式:

<Style TargetType="Button" BasedOn="{StaticResource {x:Type Control}}" x:Key="OnButton">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border CornerRadius="15" Background="{StaticResource GradientBrushOn}" BorderThickness="1" Padding="2">
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style TargetType="Button" BasedOn="{StaticResource OnButton}" x:Key="OffButton">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border CornerRadius="15" Background="{StaticResource GradientBrushOff}" BorderThickness="1" Padding="2" x:Name="TheBorder">
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

长话短说,我正在寻找一种方法来缩短 OffButton 样式,这样它只会更改 OnButton 样式的背景 属性 来自 GradientBrushOnGradientBruthOff

这是我第一次使用 WPF,所以我认为这应该是一件相当基础的事情,但是我就是找不到任何方法来做到这一点,即使在 google 上花了 2 个小时之后也是如此。

使用 Button.Background 属性 并在 ControlTemplate 中使用 TemplateBinding

<Style TargetType="Button" BasedOn="{StaticResource {x:Type Control}}" x:Key="OnButton">
    <Setter Property="Background" Value="{StaticResource GradientBrushOn}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border CornerRadius="15" Background="{TemplateBinding Background}" BorderThickness="1" Padding="2">
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style TargetType="Button" BasedOn="{StaticResource OnButton}" x:Key="OffButton">
    <Setter Property="Background" Value="{StaticResource GradientBrushOff}"/>
</Style>

如果你这样做,这也会起作用

<Button Style="..." Background="Green"/>

编辑

由于您必须在 ControlTemplate 中正常更改整个模板,因此您可以利用控件的尽可能多的内置属性,例如 BackgroundBorderBrushBorderThicknessPaddingHorizontalContentAlignmentVerticalContentAlignment 等等,因此您可以共享模板并仅调整样式中的内容,例如

<Style TargetType="Button" BasedOn="{StaticResource {x:Type Control}}" x:Key="OnButton">
   <Setter Property="Background" Value="{StaticResource GradientBrushOn}"/>
   <Setter Property="BorderThickness" Value="1"/>
   <Setter Property="Padding" Value="2"/>
   <Setter Property="HorizontalContentAlignment" Value="Center"/>
   <Setter Property="VerticalContentAlignment" Value="Center"/>
   <Setter Property="Template">
       <Setter.Value>
           <ControlTemplate TargetType="Button">
               <Border CornerRadius="15" Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" Padding="{TemplateBinding Padding}">
                   <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
               </Border>
           </ControlTemplate>
       </Setter.Value>
   </Setter>
</Style>