[英]WPF XAML - Override a button's MouseOver look
I tried all the other solutions found on this site or on others but it doesn't work. 我尝试了在本网站或其他网站上找到的所有其他解决方案,但它不起作用。 Here is button:
这是按钮:
<Button Grid.Row="1" Grid.Column="0" HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" Margin="10,10,5,10" BorderThickness="3">
<Button.Background>
<SolidColorBrush Opacity="0.0" Color="#b3b3b3"/>
</Button.Background>
<Button.BorderBrush>
<SolidColorBrush Opacity="0.3" Color="Gray"/>
</Button.BorderBrush>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="5*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
</Grid>
</Button>
What I tried: 我尝试了什么:
Addig a style in the Windows.Resources
: 在
Windows.Resources
添加样式:
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Gray"/>
</Trigger>
</Style.Triggers>
And adding this inside the button as well. 并在按钮内添加此项。 The two solutions I got was either nothing, the old highlighted look, or an error that I can't change the
Content
twice . 我得到的两个解决方案要么是没有,旧的突出显示的外观,或者我无法两次更改
Content
的错误。 As I'm out of ideas, could someone show me how to change my button's highlighted look? 由于我没有想法,有人可以告诉我如何更改按钮突出显示的外观吗?
EDIT: 编辑:
copied character by character from the thread I mentioned in my answer: 从我在答案中提到的帖子中按字符复制字符:
<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="{x:Type Button}">
<Setter Property="Background" Value="Green"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="DarkGoldenrod"/>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
Now it works. 现在它有效。 The reason it didn't was that my own button - in this question - had some of its own properties, like those
brushes
and the grid
. 它没有的原因是我自己的按钮 - 在这个问题 - 有一些自己的属性,如那些
brushes
和grid
。
Update: all solutions would work, the reason they didn't for me is that my buttons define their own style inside of them. 更新:所有解决方案都可以使用,他们没有给我的原因是我的按钮在它们内部定义了自己的风格。 Once I commented my old buttons out and added a bare button like this:
一旦我评论了我的旧按钮并添加了这样的裸按钮:
EDIT: 编辑:
Here I said define their own style
. 在这里我说
define their own style
。 It was a wrong way to call it, I meant the brushes and the grid by it. 这是一种错误的方式来称呼它,我的意思是画笔和网格。
This thread did the trick. 这个线程做了伎俩。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.