禁用 WPF 按钮上的默认动画
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19613647/
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 default animation on WPF Button
提问by jovanMeshkov
In WPF: how to disable the animation when the button after be pressed?
This thing above solves the problem, but it changes whole template, is there any more elegant solution, for example something like this:
上面这个东西解决了问题,但它改变了整个模板,有没有更优雅的解决方案,例如这样的:
<Style.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter Property="StoryBoard" Value="Disable" />
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="StoryBoard" Value="Disable" />
</Trigger>
</Style.Triggers>
采纳答案by Rohit Vats
It's not the storyboardwhich is animating the MouseOver and Pressed eventsbut instead border brush for button is updatedvia control tempalte triggers. If you want to get away with those triggers, unfortunately you have to override the template to remove those triggers from the default template.
这是not the storyboard该动画的MouseOver and Pressed events,而是border brush for button is updated通过control tempalte triggers。如果你想摆脱这些触发器,unfortunately you have to override the template to remove those triggers from the default template.
Default template can be found here. You can see the triggers there which are responsible for updating border brushes.
可以在此处找到默认模板。您可以在那里看到负责更新边框画笔的触发器。
Simply remove that triggers from default template and you are good to go. Put the style in App resources if you want that style to be applied over all buttons in your app.
只需从默认模板中删除该触发器即可。如果您希望该样式应用于应用程序中的所有按钮,请将样式放在应用程序资源中。
<Style TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border x:Name="Border"
BorderThickness="1"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}">
<ContentPresenter Margin="2"
HorizontalAlignment="Center"
VerticalAlignment="Center"
RecognizesAccessKey="True"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Apart from this in case you want to give your button a look and feel of toolbar buttons you can do that by simply applying style of toolbar button like this -
除此之外,如果您想让按钮具有工具栏按钮的外观和感觉,您可以通过简单地应用这样的工具栏按钮样式来实现 -
<Button Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}"/>
回答by Luděk Urbani?
you can also use a handler for PreviewMouseLeftButtonDown instead ButtonClick
您还可以使用 PreviewMouseLeftButtonDown 的处理程序代替 ButtonClick
function will be the same and command e.Handled = true;ensures that the animation does not start
函数将与命令e.Handled = true相同 ;确保动画不会开始
private void button_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
//some your code
e.Handled = true;
}

