代码之家  ›  专栏  ›  技术社区  ›  jovanMeshkov

禁用WPF按钮上的默认动画

  •  4
  • jovanMeshkov  · 技术社区  · 11 年前

    In WPF: how to disable the animation when the button after be pressed?

    上面的这件事解决了问题,但它改变了整个模板,有没有更优雅的解决方案,例如这样的解决方案:

    <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>
    
    2 回复  |  直到 7 年前
        1
  •  4
  •   Luděk Urbanič    10 年前

    也可以使用PreviewMouseLeftButtonDown的处理程序来代替ButtonClick

    功能和命令相同 e.处理=真; 确保动画不会启动

     private void button_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
    
                //some your code
                e.Handled = true;
            }
        2
  •  3
  •   Rohit Vats    11 年前

    它是 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 .

    可以找到默认模板 here 。你可以在那里看到负责更新边框刷的触发器。

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

    <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}}"/>