2012-07-23 41 views
2

我想改变背景(鼠标悬停)我的组合框的颜色,当我用鼠标移动它。Combobox Mouseover

我已经阅读计算器许多职位,并试图像这样的代码:

<ComboBox.Resources> 
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red"/> 
</ComboBox.Resources> 

和那结果:

enter image description here

但是,这不是我想要的。我想改变这种背景:

enter image description here

我怎样才能做到这一点? ComboBox的 只需添加刷到资源并从背景属性引用它(通过StaticResource的结合)::

回答

2

朴素解

<Window x:Class="WpfApplication2.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525"> 
<Window.Resources> 
    <SolidColorBrush x:Key="BackgroundColorKey" Color="Red"/> 
    <Style TargetType="{x:Type ComboBox}"> 
     <Style.Triggers> 
      <Trigger Property="IsMouseOver" Value="True"> 
       <Setter Property="Background" Value="{StaticResource BackgroundColorKey}" /> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 
</Window.Resources> 
<Grid> 
    <ComboBox Height="25"/> 
</Grid> 

问题: 项目之后选择了背景颜色重置为默认颜色。解决这个问题的唯一解决方案是覆盖组合框的默认模板。

修复: 修改默认组合框模板。 msdn上有组合框默认模板。这里是链接 - http://msdn.microsoft.com/en-us/library/ms752094(v=vs.100).aspx。请参阅“ComboBox ControlTemplate示例”一节。

相关链接:

MouseOver highlighting style returning to default after a second (Caused by Aero?)

http://social.msdn.microsoft.com/Forums/en/wpf/thread/a18891e9-8879-4819-9679-247341782f60

+0

的同意,我们改变背景,但我想,我们通过鼠标悬停* * – David 2012-07-23 10:17:25

+0

对不起,忘了鼠标的行为改变背景。然后,您需要为IsMouseOver属性添加具有触发器的样式。看到我更新的答案。 – 2012-07-23 10:27:20

+0

好的谢谢,我看到红色的背景,但只有一秒钟... – David 2012-07-23 10:30:43