wpf 鼠标悬停时停止按钮颜色更改
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12043523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Stop button color change on Mouseover
提问by Dannyboy
ok..this is very strange....i have defined a button, actually 5 buttons and each have a different color but on mouse over, they just change the color to that icy blue color....i tried to override that by using the below code :
好的..这很奇怪..我定义了一个按钮,实际上 5 个按钮,每个按钮都有不同的颜色,但是在鼠标悬停时,它们只是将颜色更改为冰蓝色......我试图覆盖它通过使用以下代码:
<Button Name="btn1" Content="Button" Width="65" Height="45" Background="Green" Margin="1,1,0,1" FontWeight="Bold">
<Button.Style>
<Style>
<Style.Triggers>
<Trigger Property="Button.IsMouseOver" Value="True">
<Setter Property="Button.Background" Value="Yellow" />
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
<Button.LayoutTransform>
<RotateTransform Angle="270"/>
</Button.LayoutTransform>
</Button>
but it still does not work...what i want is that it should maintain the background color it has been given(different for each button), so the question is : do i have to define it again and again for each button (the trigger did not work, color becomes ice blue) OR can i define it in the resource file with a generic value that it either stops the color change or just sets background to existing property EDIT : Just to be clear, i want the button to stop changing its color on mouseover and just retain whatever color i have assigned it.....
但它仍然不起作用......我想要的是它应该保持它已经给出的背景颜色(每个按钮不同),所以问题是:我是否必须一次又一次地为每个按钮定义它(触发器不起作用,颜色变成冰蓝色)或者我可以在资源文件中用一个通用值定义它,它要么停止颜色变化,要么只是将背景设置为现有属性编辑:只是为了清楚,我希望按钮停止在鼠标悬停时更改其颜色并保留我分配给它的任何颜色.....
采纳答案by CodingGorilla
Based on the comments and the SO post I linked: that article applied the style to anything of type Button, that's why it applies to all buttons (which is not necessarily a bad thing). But the salient point to take away from that article is that what you need to modify is the ControlTemplate.
根据我链接的评论和 SO 帖子:那篇文章将样式应用于任何类型Button,这就是它适用于所有按钮的原因(这不一定是坏事)。但要从那篇文章中删除的要点是,您需要修改的是ControlTemplate.
So you would want to do something like this:
所以你会想要做这样的事情:
<Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="Chrome" BorderBrush="Black" BorderThickness="2" CornerRadius="2" Background="{TemplateBinding Property=Background}">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
This isn't perfect, because it loses all of it's interactivity (ie. you can't tell you've pushed it, hovered over it or anything). But it gives you a good starting point.
这并不完美,因为它失去了所有的交互性(即,您无法判断是否已推动它、悬停在它上面或任何东西)。但它为您提供了一个很好的起点。
One of the key points here is that the Backgroundproperty of the Borderis bound to the TemplateParent Property=Background, that means it's going to read the background property of the button the that it is templating and use it's background color. This makes it easier for you to use a single style to cover all 5 of your buttons. You could (and may want to) do similar things with the BorderBrushproperty.
这里的关键点之一是 的Background属性Border绑定到TemplateParent Property=Background,这意味着它将读取它正在模板化的按钮的背景属性并使用它的背景颜色。这使您可以更轻松地使用单一样式来覆盖所有 5 个按钮。您可以(并且可能想要)对BorderBrush房产做类似的事情。
Also notice my style has a x:Keyvalue, so in order to use this, you would do:
还要注意我的风格有一个x:Key价值,所以为了使用它,你会这样做:
<Button x:Name="btn1" Content="Button" Width="65" Height="45" Background="Green" Margin="1,1,0,1" FontWeight="Bold" Style="{DynamicResource ButtonStyle1}">
<Button.LayoutTransform>
<RotateTransform Angle="270"/>
</Button.LayoutTransform>
</Button>
You can remove the x:Keyattribute and the style will indeed apply to all buttons inside of whichever container you declare the resource.
您可以删除该x:Key属性,并且样式确实适用于您声明资源的任何容器内的所有按钮。

