2014-05-19 26 views
0

我正在努力寻找解决方案。我试过的每一个人都没有工作。也许我错过了一些东西。设置按钮悬停或onmouseover背景色

我想改变一个按钮的背景颜色,当我用鼠标悬停在它上面。 这是我的XAML:

<Window x:Class="Recruitment_Manager.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="374" Width="940"> 
<Window.Resources> 
    <Style TargetType="Button" x:Key="NavigationButtonStyle"> 
     <Setter Property="Height" Value="35"/> 
     <Setter Property="HorizontalContentAlignment" Value="Stretch"/> 
     <Setter Property="VerticalContentAlignment" Value="Stretch"/> 
     <Setter Property="Background" Value="Transparent"/> 
     <Style.Triggers> 
      <Trigger Property="IsMouseOver" Value="True"> 
       <Setter Property="Background" Value="Green"/> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 
    <Style TargetType="StackPanel" x:Key="NavigationButtonContentStyle"> 
     <Setter Property="Orientation" Value="Horizontal"/> 
    </Style> 
    <Style TargetType="Image" x:Key="NavigationButtonImageStyle"> 
     <Setter Property="Width" Value="35"/> 
    </Style> 
    <Style TargetType="Label" x:Key="NavigationButtonLabelStyle"> 
     <Setter Property="Foreground" Value="White"/> 
     <Setter Property="FontSize" Value="15"/> 
    </Style> 
</Window.Resources> 
<Grid> 
    <StackPanel HorizontalAlignment="Left" Width="200"> 
     <StackPanel.Background> 
      <SolidColorBrush Color="#404040"/> 
     </StackPanel.Background> 
     <Button Style="{StaticResource ResourceKey=NavigationButtonStyle}"> 
      <StackPanel Style="{StaticResource ResourceKey=NavigationButtonContentStyle}"> 
       <Image Style="{StaticResource ResourceKey=NavigationButtonImageStyle}"> 

       </Image> 
       <Label Style="{StaticResource ResourceKey=NavigationButtonLabelStyle}"> 
        Settings 
       </Label> 
      </StackPanel> 
     </Button> 

    </StackPanel> 
</Grid> 

但它看起来像触发从来没有执行? 我错过了什么,或者我怎么能得到我想要的? 预先感谢您。

回答

1

要删除您将需要修改的ControlTemplate的按钮的默认鼠标悬停行为。改变你的风格定义到以下应该做的伎俩:

<Style TargetType="{x:Type Button}" x:Key="NavigationButtonStyle"> 
    <Setter Property="Height" Value="35"/> 
    <Setter Property="HorizontalContentAlignment" Value="Stretch"/> 
    <Setter Property="VerticalContentAlignment" Value="Stretch"/> 
    <Setter Property="Background" Value="Transparent"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type Button}"> 
       <Border Background="{TemplateBinding Background}"> 
        <ContentPresenter HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    <Style.Triggers> 
     <Trigger Property="IsMouseOver" Value="True"> 
      <Setter Property="Background" Value="Green"/> 
     </Trigger> 
    </Style.Triggers> 
</Style> 
+0

这项工作...只是将horizo​​ntalAlignment更改为“Stretch”及其工作的100%。感谢您的帮助。 –

0

尝试下面的代码

<Style TargetType="Button" x:Key="NavigationButtonStyle"> 
    ...  
    <Trigger Property="Control.IsMouseOver" Value="True"> 
     <Setter Property="Background" Value="Green"/> 
    </Trigger> 
</Style> 
+0

它不起作用。 –

+0

其在演示应用程序中对我的工作很好。仍然改变TargetType:“{x:Type Button}”并再次检查。 – Akshay

+0

号仍然不起作用。 –