2016-04-15 64 views
2

我使用帶有漸變背景的wpf按鈕。但背景不適用於鼠標懸停。我的代碼有什麼問題嗎?wpf按鈕懸停漸變不起作用

<Button x:Name="button1" Content="Login" HorizontalAlignment="Left" Margin="172,149,0,0" VerticalAlignment="Top" Width="91" Click="button_Click" Height="34" Foreground="Black"> 
     <Button.Style> 
      <Style TargetType="Button"> 
       <Setter Property="Background"> 
        <Setter.Value> 
         <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
          <GradientStop Color="#FF2B4702" Offset="1"/> 
          <GradientStop Color="#FFAFDE80" Offset="0"/> 
         </LinearGradientBrush> 
        </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="#FF2B4702" Offset="0"/> 
            <GradientStop Color="#FFAFDE80" Offset="1"/> 
           </LinearGradientBrush> 
          </Setter.Value> 
         </Setter> 
        </Trigger> 
       </Style.Triggers> 
      </Style> 
     </Button.Style> 
    </Button> 

回答

2

按照MSDNMouseOverBackground模板是固定的。

爲了改變你必須改變模板。

右鍵單擊您的按鈕並選擇Edit Template =>Edit a Copy

你會得到一個模板是這樣的:

<Window.Resources> 
    <ControlTemplate x:Key="ButtonBaseControlTemplate1" TargetType="{x:Type ButtonBase}"> 
     <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True"> 
      <ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> 
     </Border> 
     <ControlTemplate.Triggers> 
      <Trigger Property="Button.IsDefaulted" Value="True"> 
       <Setter Property="BorderBrush" TargetName="border" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/> 
      </Trigger> 
      <Trigger Property="IsMouseOver" Value="True"> 
       <Setter Property="Background" TargetName="border" Value="#FFBEE6FD"/> 
       <Setter Property="BorderBrush" TargetName="border" Value="#FF3C7FB1"/> 
      </Trigger> 
      <Trigger Property="IsPressed" Value="True"> 
       <Setter Property="Background" TargetName="border" Value="#FFC4E5F6"/> 
       <Setter Property="BorderBrush" TargetName="border" Value="#FF2C628B"/> 
      </Trigger> 
      <Trigger Property="ToggleButton.IsChecked" Value="True"> 
       <Setter Property="Background" TargetName="border" Value="#FFBCDDEE"/> 
       <Setter Property="BorderBrush" TargetName="border" Value="#FF245A83"/> 
      </Trigger> 
      <Trigger Property="IsEnabled" Value="False"> 
       <Setter Property="Background" TargetName="border" Value="#FFF4F4F4"/> 
       <Setter Property="BorderBrush" TargetName="border" Value="#FFADB2B5"/> 
       <Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="#FF838383"/> 
      </Trigger> 
     </ControlTemplate.Triggers> 
    </ControlTemplate> 
</Window.Resources> 

在你可以改變所有你想要的屬性模板。

+0

謝謝,我從你的回答中瞭解到。 – Neo

0

問題是你試圖修改前景,而不是原始背景。

<Style.Triggers> 
    <Trigger Property="IsMouseOver" Value="True"> 
     <Setter Property="Background"> 
      <Setter.Value> 
       <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
        <GradientStop Color="#FF2B4702" Offset="0"/> 
        <GradientStop Color="#FFAFDE80" Offset="1"/> 
       </LinearGradientBrush> 
       </Setter.Value> 
      </Setter> 
    </Trigger> 
</Style.Triggers> 
+0

不錯的斑點,但那不行。該背景必須在模板中進行更改。 –

+0

是的,我錯了,在提問時複製我的代碼。我通過改變前景而不是背景來測試。謝謝回答。 – Neo

+0

不客氣。 – allencage