如何控制 VisualState 内 Storyboard 的 SpeedRatio?

发布于 2024-09-15 02:11:51 字数 682 浏览 6 评论 0原文

给定由 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 技术交流群。

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

发布评论

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

评论(3

奶气 2024-09-22 02:11:51

对于那些来到这里寻找 UWP 解决方案的人,您可以Storyboard 的 SpeedRatio 属性上使用 {x:Bind} 它确实会按照您的预期动态改变速度比。

<Storyboard SpeedRatio="{x:Bind ViewModel.SpinningSpeedRatio, Mode=OneWay}">

编辑:需要注意的是,您需要确保在 UIThread 上设置比率,否则它将被 VisualStateManager 忽略。
Storyboard 仍会播放,但不会以您设置的速度播放。

For those arriving here looking for a UWP solution, you can use an {x:Bind} on the SpeedRatio property of a Storyboard and it does dynamically change the speed ratio as you would expect.

<Storyboard SpeedRatio="{x:Bind ViewModel.SpinningSpeedRatio, Mode=OneWay}">

Edit: One caveat is you need to ensure you set the ratio on the UIThread, otherwise it will be ignored by the VisualStateManager.
The Storyboard will still play, but not at the speed you have set.

栖迟 2024-09-22 02:11:51

通常您会使用 {TemplateBinding...} 而不是 {Binding...},但这仅适用于简单、兼容的类型。

您还应该能够使用 “相对绑定源”。如果类型不匹配,这还允许您使用值转换器。

SpeedRation={Binding SomeProperty, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource SomeConverter}}" 

我没有在 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.

SpeedRation={Binding SomeProperty, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource SomeConverter}}" 

I have not tested this in WPF, but Silverlight is normally the feature-restricted one.

指尖凝香 2024-09-22 02:11:51

好吧,看起来确实没有办法通过绑定来严格处理这个问题。因此,为了解决这个问题,我在代码隐藏中挂钩了事件来适当地启动/调整动画。

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.

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