跨多个控件同步 WPF ColorAnimation
我有几个类似切换的按钮,我希望它们在按下状态时一致跳动。
我定义了一种带有触发器的样式,可以启动发光动画,除了每个按钮与其他按钮异步脉动之外,效果很好。
如何让每个按钮的脉冲与其他按钮同步?
风格是这样的:
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的...我会尝试一下这个...
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.
实际上你应该通过资源来做到这一点,至少使用隐藏控件对我个人来说有点太多了。
它需要满足什么条件才能工作:
嗯,不可否认,为此拥有一个包装类也有点老套,但它比完全控制更干净(如果您想使用控件,您可以利用一些未使用的
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:
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)