使用附加属性在 XAML 中为 WPF 元素设置动画?
我的动画由 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:
无法解析属性路径中的所有属性引用画布.顶部.
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.