WPF-使按钮的下沉效果变慢

时间:2017-12-19 04:46:56

标签: c# wpf xaml

我在WPF中有一个按钮,当单击该按钮时会创建一个凹陷效果。我想知道是否有一个属性使效果显得更慢而不是默认速度?

这是我的代码:

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

        }

2 个答案:

答案 0 :(得分:4)

使用厚度动画而不是显式的PreviewMouseDownEvent

参考以下示例

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

答案 1 :(得分:2)

您可以使用厚度动画:

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);
}

<强>编辑:

由于您希望按钮返回默认位置

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);
}