(这是试图解决我之前的问题 https://stackoverflow.com/questions/5826828/how-to-start-stop-animation-in-user-control-from-view-model以不同的方式。)
我创建了一个使用 RadialGradientBrush 的用户控件,我希望能够通过在视图模型上设置属性来为其设置动画。渐变画笔还具有一些绑定到视图模型的属性。
用户控件的 XAML 是(为了简洁起见,剪掉了一些属性):
<UserControl x:Class="WpfApplication1.AnimatedLineArrow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:Controls="clr-namespace:Microsoft.Expression.Controls;assembly=Microsoft.Expression.Drawing"
mc:Ignorable="d" d:DesignHeight="150" d:DesignWidth="300"
Name="animatedLineArrow">
<Grid>
<Controls:LineArrow x:Name="ArrowControl"
StartCorner="{Binding ElementName=animatedLineArrow, Path=StartCorner, FallbackValue=TopRight}"
Width="{Binding ElementName=animatedLineArrow, Path=Width, FallbackValue=200}"
Height="{Binding ElementName=animatedLineArrow, Path=Height, FallbackValue=200}"
<Controls:LineArrow.Stroke>
<RadialGradientBrush RadiusX="0.2" RadiusY="1.0"
Center="{Binding ElementName=animatedLineArrow, Path=StartPoint, Mode=OneWay}"
GradientOrigin="{Binding ElementName=animatedLineArrow, Path=StartPoint, Mode=OneWay}">
<RadialGradientBrush.GradientStops>
<GradientStop Color="{Binding ElementName=animatedLineArrow, Path=HighlightColour, Mode=OneWay, FallbackValue=Cyan}" Offset="0.0" />
<GradientStop Color="{Binding ElementName=animatedLineArrow, Path=PrimaryColour, Mode=OneWay, FallbackValue=Navy}" Offset="1.0" />
</RadialGradientBrush.GradientStops>
</RadialGradientBrush>
</Controls:LineArrow.Stroke>
</Controls:LineArrow>
</Grid>
</UserControl>
代码隐藏设置各种依赖属性,并在控件的 Loaded 事件中定义 Storyboard 来为 RadialGradientBrush 的 Center 和 GradientOrigin 属性设置动画,然后定义应响应这些依赖属性之一的值的 DataTrigger:
private void ConfigureAnimation(object sender, EventArgs e)
{
StartPoint = StartingPoints[StartCorner];
EndPoint = EndingPoints[StartCorner];
AnimatedLineArrow arrow = (AnimatedLineArrow)sender;
Storyboard storyboard = CreateStoryboard(arrow);
DataTrigger startTrigger = new DataTrigger
{
Binding = new Binding
{
Path = new PropertyPath(IsRunningProperty),
RelativeSource = RelativeSource.Self
},
Value = true
};
startTrigger.EnterActions.Add(new BeginStoryboard { Storyboard = storyboard, Name = "beginStoryboard" });
DataTrigger endTrigger = new DataTrigger
{
Binding = new Binding
{
Path = new PropertyPath(IsRunningProperty),
RelativeSource = RelativeSource.Self
},
Value = false
};
endTrigger.EnterActions.Add(new StopStoryboard { BeginStoryboardName = "beginStoryboard" });
Style style = new Style(typeof(AnimatedLineArrow));
style.Triggers.Add(startTrigger);
style.Triggers.Add(endTrigger);
arrow.Style = style;
}
private Storyboard CreateStoryboard(AnimatedLineArrow arrow)
{
Storyboard storyboard = new Storyboard();
PointAnimation originAnimation = new PointAnimation(StartingPoints[StartCorner], EndingPoints[StartCorner], Duration, FillBehavior.HoldEnd);
PointAnimation centreAnimation = originAnimation.Clone();
Storyboard.SetTarget(originAnimation, arrow);
Storyboard.SetTargetProperty(originAnimation, new PropertyPath(RadialGradientBrush.GradientOriginProperty));
Storyboard.SetTarget(centreAnimation, arrow);
Storyboard.SetTargetProperty(centreAnimation, new PropertyPath(RadialGradientBrush.CenterProperty));
storyboard.Children.Add(originAnimation);
storyboard.Children.Add(centreAnimation);
return storyboard;
}
当我尝试运行该项目时,它编译成功,窗口成功加载,控件处于默认状态。但是,当 DataTrigger 第一次触发时,我收到以下异常:
System.InvalidOperationException was unhandled
Message='beginStoryboard' name cannot be found in the name scope of 'System.Windows.Style'.
Source=PresentationFramework
我附上了一个示例项目展示我想要实现的目标 http://dl.dropbox.com/u/8139802/WpfAnimatedArrow.zip.