使用附加属性在XAML中对WPF元素进行动画处理? [英] Animating WPF element in XAML using attached property?
问题描述
我的动画可以通过ViewModel中的一个属性触发来工作。如果我将 TargetProperty
设置为 Width
,则以下代码实际上可用于生成图像。
I got my animation to work triggered by a property in my ViewModel. If I set my TargetProperty
to Width
, the below code actually works in growing the image.
接下来,我想实际上下移动图像。为此,我在图像周围添加了 Canvas
组件,以便能够基于 Canvas.Top
属性进行动画处理。在图像上设置 Canvas.Top
会将其移动到我想要的位置。
Next, I wanted to actually move the image up and down. To do this, I added a Canvas
component around my image, to be able to animate based on Canvas.Top
property. Setting Canvas.Top
on the image moves it where I want.
但是,如果我设置了 StoryBoard.TargetProperty
到 Canvas.Top
,我得到一个错误:
However, if I set my StoryBoard.TargetProperty
to Canvas.Top
, I get an error:
无法解析属性路径
Canvas.Top中的所有属性引用。
Cannot resolve all property references in the property path Canvas.Top.
<Style x:Key="LoadingImageAnimation" TargetType="{x:Type Image}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsLoading}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation From="10" To="250" AutoReverse="True" Duration="0:0:30"
Storyboard.TargetProperty="Canvas.Top"/>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>
我的方法是否完全无效,还是只是寻找附属财产?
Is my approach totally off, or just a matter of finding the Attached Property?
推荐答案
在属性路径语法。并且解决方案实际上很简单。需要添加括号(Canvas.Top)。
did some digging around on Property Path Syntax. and the solution was actually simple.. Needed to add parentheses "(Canvas.Top)".
动画不如我所希望的那样平滑..但至少现在有效。
The animation is not as smooth as i would like.. but at least it works now.
这篇关于使用附加属性在XAML中对WPF元素进行动画处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!