如何改变在WPF一个按钮鼠标悬停背景? [英] How do you change Background for a Button MouseOver in WPF?
问题描述
我有这个XAML我的网页上的按钮:
I have a button on my page with this XAML:
<Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Bottom"
Width="50" Height="50" HorizontalContentAlignment="Left"
BorderBrush="{x:Null}" Foreground="{x:Null}" Margin="50,0,0,0">
<Button.Style>
<Style TargetType="Button">
<Setter Property="Background" Value="Green"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
但是,当我把鼠标放在我的按钮,按钮的背景变为默认的Windows灰色背景。照片有什么问题?结果
But when I put mouse over my button, button's background changes to default windows gray background.
What's The Problem?
这是之前和之后,鼠标悬停按钮图片:结果
前:结果结果
后:结果
结果
This is the button picture before and after mouseover:
Before:
After:
推荐答案
要删除默认的鼠标悬停
在行为的按钮
您将需要修改控件模板
。更改风格
定义下面应该做的伎俩:
To remove the default MouseOver
behaviour on the Button
you will need to modify the ControlTemplate
. Changing your Style
definition to the following should do the trick:
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="Green"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}" BorderBrush="Black" BorderThickness="1">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
编辑:这几年较晚,但你实际上可以设置在那里的边境内的边框刷。 IDK如果被指出,但它似乎并不像它...
It's a few years late, but you are actually able to set the border brush inside of the border that is in there. Idk if that was pointed out but it doesn't seem like it was...
这篇关于如何改变在WPF一个按钮鼠标悬停背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!