3

我坚持使用 WPF 中的样式。当用户将鼠标悬停在按钮上时,我需要更改背景渐变,但我不知道如何进行更改。这是我到目前为止的代码:

    <Style x:Key="Button_Green" TargetType="{x:Type Button}">
    <Setter Property="FontSize" Value="11" />
    <Setter Property="Foreground" Value="White"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border CornerRadius="2" BorderBrush="#387f38" BorderThickness="1">
                    <Border.Background>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="#FF5BB75B" Offset="0" />
                            <GradientStop Color="#FF449B44" Offset="1" />
                        </LinearGradientBrush>
                    </Border.Background>
                    <ContentPresenter x:Name="ButtonContentPresenter" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background">
                <Setter.Value>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="#FF5BB75B" Offset="0" />
                        <GradientStop Color="#FF398239" Offset="1" />
                    </LinearGradientBrush>
                </Setter.Value>
            </Setter>
            <Setter Property="Cursor" Value="Hand" />
        </Trigger>
    </Style.Triggers>
</Style>

鼠标光标正常变化,但背景渐变没有变化。

4

2 回答 2

4

您需要将 setter 的目标设置为Border您在ControlTemplate. 目前,它的目标是自身的Background属性,该属性Button被您的 custom 隐藏ControlTemplate

<Style x:Key="Button_Green" TargetType="{x:Type Button}">
<Setter Property="FontSize" Value="11" />
<Setter Property="Foreground" Value="White"/>
<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="{x:Type Button}">
            <Border x:Name="MyBackgroundElement" CornerRadius="2" BorderBrush="#387f38" BorderThickness="1">
                <Border.Background>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="#FF5BB75B" Offset="0" />
                        <GradientStop Color="#FF449B44" Offset="1" />
                    </LinearGradientBrush>
                </Border.Background>
                <ContentPresenter x:Name="ButtonContentPresenter" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter TargetName="MyBackgroundElement" Property="Background">
                        <Setter.Value>
                            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                <GradientStop Color="#FF5BB75B" Offset="0" />
                                <GradientStop Color="#FF398239" Offset="1" />
                            </LinearGradientBrush>
                        </Setter.Value>
                    </Setter>
                    <Setter Property="Cursor" Value="Hand" />
               </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Setter.Value>
</Setter>
</Style>

在这里,我x:Name为元素添加了一个属性,Border以便可以TargetName使用Setter.

于 2012-05-27T21:44:04.843 回答
0

从模板更改此代码段

<Border CornerRadius="2" BorderBrush="#387f38" BorderThickness="1">
   <Border.Background>
       <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
           <GradientStop Color="#FF5BB75B" Offset="0" />
           <GradientStop Color="#FF449B44" Offset="1" />
       </LinearGradientBrush>
   </Border.Background>
   <ContentPresenter x:Name="ButtonContentPresenter" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Border>

对此(重要的部分是Background="{TemplateBinding Background}"):

<Border CornerRadius="2" BorderBrush="#387f38" BorderThickness="1" Background="{TemplateBinding Background}">       
   <ContentPresenter x:Name="ButtonContentPresenter" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Border>

然后像这样为您的样式添加一个额外的设置器:

<Setter Property="Background">
  <Setter.Value>
      <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
          <GradientStop Color="#FF5BB75B" Offset="0" />
          <GradientStop Color="#FF449B44" Offset="1" />
      </LinearGradientBrush>
  </Setter.Value>
</Setter>
于 2012-05-28T00:29:22.203 回答