如何使 WPF 按钮振动?

How to make a WPF button vibrate?

我正在构建一个包含多个按钮的 Windows WPF 应用程序。 我想通知用户他应该加载一个 pdf,然后才能按下其他按钮,方法是如果用户单击其他地方并且没有加载 pdf,则使加载 pdf 按钮闪烁/振动。

例如 - 如果您尝试在打开的编辑颜色框外的任何地方单击,Microsoft Paint 会发生确切的行为。 (见附件 gif)

有人知道吗?

您可以使用带有 DoubleAnimation 的故事板,更改阴影,使其自动返回并重复(此处为 4 次)。

这是一个UserControl,您可以将其重复用于具有这种效果的所有按钮;您只需要将文本“按钮内容”替换为一些 DependencyProperty 即可使其通用。

<UserControl x:Class="AnimateDropShadow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      mc:Ignorable="d">
    <Grid>
        <Button HorizontalAlignment="Left" >
            Button content
            <Button.Effect>
                <DropShadowEffect x:Name="warningEffect" Color="Black" BlurRadius="10"  ShadowDepth="0"/>
            </Button.Effect>
            <Button.Triggers>
                <EventTrigger RoutedEvent="Button.Click">
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation
                                Storyboard.TargetName="warningEffect"
                                Storyboard.TargetProperty="BlurRadius"
                                From="15" To="10" Duration="0:0:0.1"
                                AutoReverse="True" RepeatBehavior="0:0:0.4" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Button.Triggers>
        </Button>
    </Grid>
</UserControl>