1

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

所以我想出了这些样式:

<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样式的 Background 属性从更改GradientBrushOnGradientBruthOff

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

4

1 回答 1

5

使用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您将使用控件的尽可能多的内置属性,例如Background, BorderBrush, BorderThickness, Padding,等HorizontalContentAlignmentVerticalContentAlignment因此您可以共享模板并仅调整样式中的内容

<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>
于 2016-02-01T14:32:21.313 回答