wpf 禁用按钮鼠标悬停效果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13141200/
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
Disable button mouse over effect
提问by Kev Fixes
I have a wppf button with a background image.
我有一个带有背景图像的 wppf 按钮。
When I mouse over background will be empty and a button is shown.
当我将鼠标悬停在背景上时,背景将为空并显示一个按钮。
How can I disable mouse effect?
如何禁用鼠标效果?
<Button BorderBrush="{x:Null}" Content="Reset" BorderThickness="0"Foreground="White" Focusable="False">
<Button.Background>
<ImageBrush ImageSource="button.png" />
</Button.Background>
回答by Joe
Here's how I disable the visual mouseover effects on buttons. I left in some of my settings just to get you a feel of how to play with the triggers and stuff, feel free to experiment!
这是我如何禁用按钮上的视觉鼠标悬停效果。我保留了一些设置只是为了让您了解如何使用触发器和其他东西,请随意尝试!
<Style TargetType="Button" x:Key="ImageButton" BasedOn="{StaticResource {x:Static ToolBar.ButtonStyleKey}}">
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Name="border"
BorderThickness="{TemplateBinding BorderThickness}"
Padding="{TemplateBinding Padding}"
BorderBrush="{TemplateBinding BorderBrush}"
CornerRadius="5"
Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="BorderBrush" Value="Gainsboro" />
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Opacity" Value="0.25" />
<Setter Property="BorderBrush" Value="Transparent" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
EDIT: Using the "BasedOn" + setting FocusVisualStyle to null (first 2 lines) gets rid of the mouse over effects. Everything else is just examples. I added a border there in order to play with it through a trigger (since I want a custom mouseover effect).
编辑:使用“BasedOn”+ 将 FocusVisualStyle 设置为 null(前 2 行)可以消除鼠标悬停效果。其他一切都只是例子。我在那里添加了一个边框,以便通过触发器来使用它(因为我想要一个自定义的鼠标悬停效果)。
回答by MrDosu
In the ControlTemplateof the Buttonthe Background property will be overridden by a trigger.
You can either redo the whole ControlTemplateand leave out the Background change or just add the image as content of your Buttonlike so to avoid all these complications:
在ControlTemplate中的Button背景属性将被触发覆盖。
您可以重做整体ControlTemplate并省略背景更改,也可以将图像添加为您Button喜欢的内容,以避免所有这些并发症:
<Button.Content>
<Image Source="button.png" />
</Button.Content>
Or if you need the text in there as well:
或者,如果您还需要那里的文字:
<Button.Content>
<Grid>
<Image Source="button.png" />
<TextBlock Text="Reset" VerticalAlignment="Center" HorizontalAlignment="Center" />
<Grid>
</Button.Content>

