组合框鼠标悬停 [英] Combobox Mouseover
问题描述
我想要改变我的组合框背景( mouseover )在stackoverflow上阅读很多帖子,并尝试过这样的代码:
< ComboBox.Resources>
< SolidColorBrush x:Key ={x:Static SystemColors.HighlightBrushKey}Color =Red/>
< /ComboBox.Resources>
,结果如下:
img src =https://i.stack.imgur.com/gtZLP.pngalt =enter image description here>
但这不是我想要的。我想更改此背景:
我该如何做?
Naive Solution :
只需向资源中添加刷子并通过ComboBox的Background属性(通过StaticResource绑定)引用它:
< Window x:Class =WpfApplication2.MainWindow
xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns :x =http://schemas.microsoft.com/winfx/2006/xaml
Title =MainWindowHeight =350Width =525>
< Window.Resources>
< SolidColorBrush x:Key =BackgroundColorKeyColor =Red/>
< Style TargetType ={x:Type ComboBox}>
< Style.Triggers>
< Trigger Property =IsMouseOverValue =True>
< Setter Property =BackgroundValue ={StaticResource BackgroundColorKey}/>
< / Trigger>
< / Style.Triggers>
< / Style>
< /Window.Resources>
< Grid>
< ComboBox Height =25/>
< / Grid>
问题:
背景颜色将重置为默认颜色。解决这个问题的唯一解决方案是覆盖组合框的默认模板。
修复:
修改默认的ComboBox模板。
msdn上存在ComboBox默认模板。以下是链接 - http://msdn.microsoft .com / en-us / library / ms752094(v = vs.100).aspx 。请参阅ComboBox ControlTemplate示例部分。
相关链接:
http://social.msdn.microsoft.com/Forums/en/wpf/thread/a18891e9-8879-4819-9679-247341782f60
I would like to change the background (mouseover) color of my combobox when i move with a mouse over it.
I have read many post on stackoverflow and have tried a code like this:
<ComboBox.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red"/>
</ComboBox.Resources>
and thats the result:
But that's not what I wanted. I want to change this background:
How can I do that?
Naive Solution: Just add brush to the resources and reference it from the Background property (via StaticResource binding) of the ComboBox:
<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>
Problem: After item is selected the background color is reset back to the default color. The only solution to fix this is to override the default template of the combobox.
Fix: Modify default ComboBox template. ComboBox default template is present on msdn. Here is the link - http://msdn.microsoft.com/en-us/library/ms752094(v=vs.100).aspx. See "ComboBox ControlTemplate Example" section.
Related links:
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
这篇关于组合框鼠标悬停的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!