WPF - Reduces the flooding effect for a button

I have a button in WPF that creates a flood effect when a button is clicked. I was wondering if there is a property to make the effect slower than the default speed?

Here is my code:

   private void btnEnglish_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            btnEnglish.BorderThickness = new Thickness(10, 10, 0, 0);

        }
+4
source share
2 answers

You can use thickness animation:

private void btnEnglish_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
   ThicknessAnimation myThicknessAnimation = new ThicknessAnimation();
   myThicknessAnimation.Duration = TimeSpan.FromSeconds(1);
   myThicknessAnimation.From = new Thickness(0,0,0,0);
   myThicknessAnimation.To = new Thickness(10,10,0,0);       

   btnEnglish.BeginAnimation(Button.BorderThicknessProperty, myThicknessAnimation);
}

Edited by:

Since you want your button to return to the default location

private void btnEnglish_Click(object sender, RoutedEventArgs e)
{
   ThicknessAnimation myThicknessAnimation = new ThicknessAnimation();
   myThicknessAnimation.Duration = TimeSpan.FromSeconds(0.5);
   myThicknessAnimation.From = new Thickness(0,0,0,0);
   myThicknessAnimation.To = new Thickness(10,10,0,0);
   myThicknessAnimation.AutoReverse = true;

   myThicknessAnimation.Completed += (arg, s) => { btnEnglish.BeginAnimation(Button.BorderThicknessProperty, null); };
   btnEnglish.BeginAnimation(Button.BorderThicknessProperty, myThicknessAnimation);
}
+2
source

Use thickness animations instead of explicit PreviewMouseDownEvent

see example

<EventTrigger RoutedEvent="Button.PreviewMouseDown">
                <BeginStoryboard>
                    <Storyboard>
                        <ThicknessAnimation
                                    Storyboard.TargetName="btnEnglish"
            Storyboard.TargetProperty="(Button.BorderThickness)" SpeedRatio="1" From="0"
            To="10,10,0,0" Duration="0:0:3" />
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
+4
source

Source: https://habr.com/ru/post/1691004/


All Articles