jovanMeshkov
jovanMeshkov

Reputation: 797

Disable default animation on WPF Button

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>

Upvotes: 4

Views: 8173

Answers (2)

Rohit Vats
Rohit Vats

Reputation: 81233

It's not the storyboard which is animating the MouseOver and Pressed events, but instead BorderBrush for button is updated via ControlTemplate triggers. If you want to get away from those 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}}"/>        

Upvotes: 4

Luděk Urbanič
Luděk Urbanič

Reputation: 41

you can also use a handler for PreviewMouseLeftButtonDown instead ButtonClick

function will be the same and command e.Handled = true; ensures that the animation does not start

 private void button_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {

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

Upvotes: 4

Related Questions