如何控制 VisualState 内 Storyboard 的 SpeedRatio?
给定由 VisualStateManager 作为 ControlTemplate 的一部分启动的 Storyboard,我将如何根据控件的属性更改调整该动画的 SpeedRatio?
<ControlTemplate>
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup>
<VisualState>
<Storyboard Name="SpinningThing"
SpeedRatio="{Binding SpinningSpeedRatio}">
...
这需要在 WPF 和 Silverlight 中都能工作。
由于多种原因,我认为我无法在那里设置绑定。最重要的是,Storyboard 是 Freezable,因此您不能在 WPF 中随意设置 SpeedRatio。但是,如果它是由 VisualStateManager 启动的,我可以对其调用 SetSpeedRatio 吗?
另外,由于它的父级是 VisualState,这是否意味着没有与之相关的管理 FrameworkElement?
那么,如果我不能通过绑定来做到这一点,该怎么办呢?
Given a Storyboard started by the VisualStateManager as part of a ControlTemplate, how would I adjust the SpeedRatio of that animation based on property changes of the control?
<ControlTemplate>
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup>
<VisualState>
<Storyboard Name="SpinningThing"
SpeedRatio="{Binding SpinningSpeedRatio}">
...
This needs to work in both WPF and Silverlight.
I don't think I can set a binding there for a number of reasons. Foremost, Storyboard is Freezable so you can't just go setting the SpeedRatio all willy-nilly in WPF. But, if it's started by the VisualStateManager, can I call SetSpeedRatio on it?
Also, since its parent is a VisualState, doesn't that mean there would be no governing FrameworkElement to relate to for it?
So, if I can't do it with a binding, how can this be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于那些来到这里寻找 UWP 解决方案的人,您可以在
Storyboard 的
它确实会按照您的预期动态改变速度比。SpeedRatio
属性上使用{x:Bind}
编辑:需要注意的是,您需要确保在
UIThread
上设置比率,否则它将被VisualStateManager
忽略。Storyboard
仍会播放,但不会以您设置的速度播放。For those arriving here looking for a UWP solution, you can use an
{x:Bind}
on theSpeedRatio
property of aStoryboard
and it does dynamically change the speed ratio as you would expect.Edit: One caveat is you need to ensure you set the ratio on the
UIThread
, otherwise it will be ignored by theVisualStateManager
.The
Storyboard
will still play, but not at the speed you have set.通常您会使用 {TemplateBinding...} 而不是 {Binding...},但这仅适用于简单、兼容的类型。
您还应该能够使用 “相对绑定源”。如果类型不匹配,这还允许您使用值转换器。
我没有在 WPF 中测试过这一点,但 Silverlight 通常是功能受限的。
Normally you would use a {TemplateBinding...} rather than a {Binding...}, but that only works for simple, compatible, types.
You should also be able to bind to the templated control using a "relative binding source". This also allows you to use a value convertor if the types do not match.
I have not tested this in WPF, but Silverlight is normally the feature-restricted one.
好吧,看起来确实没有办法通过绑定来严格处理这个问题。因此,为了解决这个问题,我在代码隐藏中挂钩了事件来适当地启动/调整动画。
Alright, so it looks like there really is no way to handle this strictly with a binding. So to account for it, I have hooked events in the code-behind to start/adjust the animations as appropriate.