如何删除 WPF 按钮上的默认鼠标悬停效果?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3854317/
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
How to remove default mouse-over effect on WPF buttons?
提问by Nellius
My problem is that in WPF, whenever I try and change the colour of a button's background using triggers or animations, the default mouseover effect (of being grey with that orange glow) seems to take priority.
我的问题是,在 WPF 中,每当我尝试使用触发器或动画更改按钮背景的颜色时,默认的鼠标悬停效果(带有橙色发光的灰色)似乎优先。
After extensive searches I'm clueless as to how to remove this effect.
经过广泛的搜索,我对如何消除这种影响一无所知。
采纳答案by Mark Heath
回答by dodgy_coder
This is similar to the solution referred by Mark Heath but with not as much code to just create a very basic button, without the built-in mouse over animation effect. It preserves a simple mouse over effect of showing the button border in black. The style can be inserted into the Window.Resources or UserControl.Resources section for example (as shown).
这类似于 Mark Heath 提到的解决方案,但没有那么多代码来创建一个非常基本的按钮,没有内置的鼠标悬停动画效果。它保留了将按钮边框显示为黑色的简单鼠标悬停效果。例如,可以将样式插入 Window.Resources 或 UserControl.Resources 部分(如图所示)。
<UserControl.Resources>
<!-- This style is used for buttons, to remove the WPF default 'animated' mouse over effect -->
<Style x:Key="MyButtonStyle" TargetType="Button">
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="Margin" Value="5"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Name="border"
BorderThickness="1"
Padding="4,2"
BorderBrush="DarkGray"
CornerRadius="3"
Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="border" Property="BorderBrush" Value="Black" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<!-- usage in xaml -->
<Button Style="{StaticResource MyButtonStyle}">Hello!</Button>
回答by Lyall
Just to add a very simple solution, that was good enough for me, and I thinkaddresses the OP's issue. I used the solution in this answerexcept with a regular Background
value instead of an image.
只是添加一个非常简单的解决方案,这对我来说已经足够了,我认为解决了 OP 的问题。我在这个答案中使用了解决方案,除了使用常规Background
值而不是图像。
<Style x:Key="SomeButtonStyle" TargetType="Button">
<Setter Property="Background" Value="Transparent" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid Background="{TemplateBinding Background}">
<ContentPresenter />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
No re-templating beyond forcing the Background
to always be the Transparent
background from the templated button - mouseover no longer affects the background once this is done. Obviously replace Transparent
with any preferred value.
除了强制Background
始终是Transparent
模板化按钮的背景外,无需重新模板 - 一旦完成,鼠标悬停不再影响背景。显然替换Transparent
为任何首选值。
回答by Benji-Man
The Muffin Man had a very simple answer which worked for me.
松饼人有一个非常简单的答案,对我有用。
To add a little more specific direction, at least for VS 2013:
要添加更具体的方向,至少对于 VS 2013:
- Right-click the control
- Select Edit Template => Edit a copy...
- I selected 'Application' for where to save the style
- From here you can directly edit App.xaml and see the intuitively named properties. For my purposes, I just set RenderMouseOver="False"
- Then, in the MainWindow.xaml or wherever your GUI is, you can paste the new style at the end of the Button tag, e.g.
... Style="{DynamicResource MouseOverNonDefault}"/>
- 右键单击控件
- 选择编辑模板 => 编辑副本...
- 我选择了“应用程序”作为保存样式的位置
- 从这里您可以直接编辑 App.xaml 并查看直观命名的属性。出于我的目的,我只是设置了 RenderMouseOver="False"
- 然后,在 MainWindow.xaml 或您的 GUI 所在的任何地方,您可以将新样式粘贴到 Button 标记的末尾,例如
... Style="{DynamicResource MouseOverNonDefault}"/>
回答by Arash.Zandi
This Link helped me alot http://www.codescratcher.com/wpf/remove-default-mouse-over-effect-on-wpf-buttons/
这个链接帮了我很多 http://www.codescratcher.com/wpf/remove-default-mouse-over-effect-on-wpf-buttons/
Define a style in UserControl.Resourcesor Window.Resources
在UserControl.Resources或Window.Resources 中定义样式
<Window.Resources>
<Style x:Key="MyButton" TargetType="Button">
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="Cursor" Value="Hand" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Name="border" BorderThickness="0" BorderBrush="Black" Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Opacity" Value="0.8" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
Then add the style to your button this way Style="{StaticResource MyButton}"
然后以这种方式将样式添加到您的按钮Style="{StaticResource MyButton}"
<Button Name="btnSecond" Width="350" Height="120" Margin="15" Style="{StaticResource MyButton}">
<Button.Background>
<ImageBrush ImageSource="/Remove_Default_Button_Effect;component/Images/WithStyle.jpg"></ImageBrush>
</Button.Background>
</Button>
回答by Prateek Rai
If someone doesn't want to override default Control Template then here is the solution.
如果有人不想覆盖默认控制模板,那么这里是解决方案。
You can create DataTemplate for button which can have TextBlock and then you can write Property trigger on IsMouseOver property to disable mouse over effect. Height of TextBlock and Button should be same.
您可以为可以具有 TextBlock 的按钮创建 DataTemplate,然后您可以在 IsMouseOver 属性上编写属性触发器以禁用鼠标悬停效果。TextBlock 和 Button 的高度应该相同。
<Button Background="Black" Margin="0" Padding="0" BorderThickness="0" Cursor="Hand" Height="20">
<Button.ContentTemplate>
<DataTemplate>
<TextBlock Text="GO" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center" TextDecorations="Underline" Margin="0" Padding="0" Height="20">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<Trigger Property ="IsMouseOver" Value="True">
<Setter Property= "Background" Value="Black"/>
</Trigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</DataTemplate>
</Button.ContentTemplate>
</Button>
回答by stoj
An extension on dodgy_coder's answer which adds support for..
dodgy_coder 答案的扩展,增加了对..
- Maintaining WPF button style
Adds support for IsSelected and hover, i.e. a toggled button
<Style x:Key="Button.Hoverless" TargetType="{x:Type ButtonBase}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ButtonBase}"> <Border Name="border" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}"> <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" /> </Border> <ControlTemplate.Triggers> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="IsMouseOver" Value="True" /> <Condition Property="Selector.IsSelected" Value="False" /> </MultiTrigger.Conditions> <Setter Property="Background" Value="#FFBEE6FD" /> </MultiTrigger> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="IsMouseOver" Value="True" /> <Condition Property="Selector.IsSelected" Value="True" /> </MultiTrigger.Conditions> <Setter Property="Background" Value="#BB90EE90" /> </MultiTrigger> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="IsMouseOver" Value="False" /> <Condition Property="Selector.IsSelected" Value="True" /> </MultiTrigger.Conditions> <Setter Property="Background" Value="LightGreen" /> </MultiTrigger> <Trigger Property="IsPressed" Value="True"> <Setter TargetName="border" Property="Opacity" Value="0.95" /> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style>
- 保持 WPF 按钮样式
添加对 IsSelected 和悬停的支持,即切换按钮
<Style x:Key="Button.Hoverless" TargetType="{x:Type ButtonBase}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ButtonBase}"> <Border Name="border" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}"> <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" /> </Border> <ControlTemplate.Triggers> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="IsMouseOver" Value="True" /> <Condition Property="Selector.IsSelected" Value="False" /> </MultiTrigger.Conditions> <Setter Property="Background" Value="#FFBEE6FD" /> </MultiTrigger> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="IsMouseOver" Value="True" /> <Condition Property="Selector.IsSelected" Value="True" /> </MultiTrigger.Conditions> <Setter Property="Background" Value="#BB90EE90" /> </MultiTrigger> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="IsMouseOver" Value="False" /> <Condition Property="Selector.IsSelected" Value="True" /> </MultiTrigger.Conditions> <Setter Property="Background" Value="LightGreen" /> </MultiTrigger> <Trigger Property="IsPressed" Value="True"> <Setter TargetName="border" Property="Opacity" Value="0.95" /> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style>
examples..
例子..
<Button Content="Wipe On" Selector.IsSelected="True" />
<Button Content="Wipe Off" Selector.IsSelected="False" />
<Button Content="Wipe On" Selector.IsSelected="True" />
<Button Content="Wipe Off" Selector.IsSelected="False" />
回答by Laine Mikael
Using a template trigger:
使用模板触发器:
<Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Background" Value="White"></Setter>
...
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="White"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>