绑定属性的 XAML 动画

发布于 2024-09-14 21:05:31 字数 359 浏览 0 评论 0原文

我将以下矩形绑定到不断变化的定位属性“RectTop”,

<Rectangle Canvas.Top="{Binding RectTop}" Height="100" Width="100" Fill="Red" />

有没有办法可以设置动画/触发器,以便在该值发生变化时将矩形平滑地动画到 RectTop? RectTop 不断变化。 RectTop 的类实现了 INotifyPropertyChanged,因此我的更新工作正常。我已经成功地使用了一个中间线程,该线程缓慢地增加 RectTop,然后调用 UI 线程来通知它的值发生变化。这种方法感觉非常hackish。一定有更好的方法。

有什么想法吗?

I have the following Rectangle bound to an ever-changing positioning property "RectTop"

<Rectangle Canvas.Top="{Binding RectTop}" Height="100" Width="100" Fill="Red" />

Is there I way I can set up an animation/trigger to smoothly animate the rectangle to RectTop whenever that value changes? RectTop changes constantly. RectTop's class implements INotifyPropertyChanged so my updates are working fine. I've had success using an intermediary thread that slowly increments RectTop, then invokes the UI thread to notify it's value changing. That method feels terribly hackish. There must be a better way.

Any ideas?

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

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

发布评论

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

评论(1

尐籹人 2024-09-21 21:05:31

简单的技巧。更新 Rectangle 的 Margin 属性,而不是更新 Canvas.Top。

而不是这个:

  <Canvas Width="300" Height="300" Background="Black">  
    <Rectangle Canvas.Top="10" Fill="Blue" Height="100" Width="100"/>
  </Canvas>

尝试这个:

<Canvas Width="300" Height="300" Background="Black">  
    <Rectangle Margin="0,10,0,0" Fill="Blue" Height="100" Width="100"/>
  </Canvas>

PS:您应该使用 厚度动画

HTH

Simple trick. Update the Margin property of the Rectangle instead of updating the Canvas.Top.

Instead of this:

  <Canvas Width="300" Height="300" Background="Black">  
    <Rectangle Canvas.Top="10" Fill="Blue" Height="100" Width="100"/>
  </Canvas>

Try this:

<Canvas Width="300" Height="300" Background="Black">  
    <Rectangle Margin="0,10,0,0" Fill="Blue" Height="100" Width="100"/>
  </Canvas>

PS: You should use Thickness Animation

HTH

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