跨多个控件同步 WPF ColorAnimation

发布于 2024-10-12 14:18:08 字数 942 浏览 3 评论 0原文

我有几个类似切换的按钮,我希望它们在按下状态时一致跳动。

我定义了一种带有触发器的样式,可以启动发光动画,除了每个按钮与其他按钮异步脉动之外,效果很好。

如何让每个按钮的脉冲与其他按钮同步?

风格是这样的:

<Storyboard x:Key="pulseStory">
    <ColorAnimation                                                                    
        Storyboard.TargetProperty="(Control.Background).(SolidColorBrush.Color)"
                    From="Red"
                    To="Transparent"
                    Duration="0:0:1" />
</Storyboard>

<Style x:Key="pulseButton" TargetType="Button">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Tag,RelativeSource={RelativeSource Self}}" Value="True">
            <DataTrigger.EnterActions>
                <BeginStoryboard Storyboard="{StaticResource pulseStory}"/>
            </DataTrigger.EnterActions>
        </DataTrigger>
    </Style.Triggers>
</Style>

干杯!

I have several toggle-like buttons that I want to pulsate in unison when in the pressed state.

I have defined a style with a trigger that kicks-off the glow animation and this works just fine, apart from the fact that each button pulsates asynchronously from the others.

How can I have each button synchronize its pulse to the others?

Here's the style:

<Storyboard x:Key="pulseStory">
    <ColorAnimation                                                                    
        Storyboard.TargetProperty="(Control.Background).(SolidColorBrush.Color)"
                    From="Red"
                    To="Transparent"
                    Duration="0:0:1" />
</Storyboard>

<Style x:Key="pulseButton" TargetType="Button">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Tag,RelativeSource={RelativeSource Self}}" Value="True">
            <DataTrigger.EnterActions>
                <BeginStoryboard Storyboard="{StaticResource pulseStory}"/>
            </DataTrigger.EnterActions>
        </DataTrigger>
    </Style.Triggers>
</Style>

Cheers!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终陌 2024-10-19 14:18:08

好的...我会尝试一下这个...

WPF 框架没有任何用于同步同时运行的动画的工具,因此您将不得不想出一种不同的方法。脑海中浮现出一个想法...

对情节提要中隐藏的 UI 元素的某些 Color 属性进行动画处理,然后使用 UI 绑定(即 ElementName 绑定)将每个按钮的颜色连接到该隐藏的 UI 元素。

OK ... I'll take a stab at this one ...

The WPF framework doesn't have any facility for synchronizing animations that are running concurrently, so you are going to have to come up with a different method. One idea springs to mind ...

Animate some Color property of a hidden UI element within your storyboard, then use UI binding (i.e. ElementName bindings) to connect to the Color of each of your buttons to this hidden UI element.

童话 2024-10-19 14:18:08

实际上你应该通过资源来做到这一点,至少使用隐藏控件对我个人来说有点太多了。

它需要满足什么条件才能工作:

  1. 您绑定的属性需要是 DependencyProperty,因此您的封装对象需要是 DependencyObject。
  2. 您必须像这样将对象引用为静态资源(而不是动态资源):
<DoubleAnimation
    Storyboard.Target="{StaticResource AnimationValue}"
    Storyboard.TargetProperty="(local:WrappedValue.Value)"
    To="0" Duration="0:0:1"/>

嗯,不可否认,为此拥有一个包装类也有点老套,但它比完全控制更干净(如果您想使用控件,您可以利用一些未使用的 Tag 属性,例如托管所有控件的容器的属性)

Actually you should be doing this via a resource, at least using a hidden control is a bit too much of a hack for me personally.

What needs to be met for it to work:

  1. The property you bind to needs to be a DependencyProperty, hence your enveloping object needs to be a DependencyObject.
  2. You have to reference the object as a static resource (as opposed to a dynamic resource) like this:
<DoubleAnimation
    Storyboard.Target="{StaticResource AnimationValue}"
    Storyboard.TargetProperty="(local:WrappedValue.Value)"
    To="0" Duration="0:0:1"/>

Well, admittedly it's a bit hacky as well to have a Wrapper-class for this but it's cleaner than a full control (if you want to use controls you can utilize some unused Tag-property, e.g. of the container that hosts all your controls)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文