4

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>
4

2 回答 2

4

您也可以使用 PreviewMouseLeftButtonDown 的处理程序,而不是 ButtonClick

功能将相同,命令 e.Handled = true; 确保动画不会开始

 private void button_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {

            //some your code
            e.Handled = true;
        }

于 2015-01-02T15:55:38.870 回答
3

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.

默认模板可以在这里找到。您可以在那里看到负责更新边框画笔的触发器。

只需从默认模板中删除该触发器即可。如果您希望将样式应用于应用程序中的所有按钮,请将样式放入应用程序资源中。

<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>

除此之外,如果您想让按钮具有工具栏按钮的外观和感觉,您可以通过简单地应用这样的工具栏按钮样式来做到这一点 -

<Button Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}"/>        
于 2013-10-27T09:20:58.270 回答