我有一个这样定义StoryBoard
的DoubleAnimation
内部:ResourceDictionary
<Storyboard x:Key="AngleRotation" >
<DoubleAnimation
Storyboard.TargetName="UnderOverTemplate"
Storyboard.TargetProperty=
"(RenderTransform).(TransformGroup.Children)[0].(RotateTransform.Angle)"
From="0" To="{Binding Path=AngleX1}" Duration="{Binding Path=Duration1}"/>
</Storyboard>`
AngleX1
并且Duration1
是在视图模型中定义的属性。
ResourceDictionary
包含在这样的视图中:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Resources/GamesResource.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
我正在尝试将此动画绑定到 aModelVisual3D
的RotateTransform3D.Rotation
属性,ModelVisual3D
它包含在一个ControlTemplate
名为“UnderOverTemplate”的内部,它包含在相同的内部ResourceDictionary
:
<ControlTemplate x:Key="UnderOverTemplate">
<Viewport3D>
<ModelVisual3D>
<ModelVisual3D.Children>
<ModelVisual3D>
<!--3D cube here-->
</ModelVisual3D>
</ModelVisual3D.Children>
<ModelVisual3D.Transform>
<Transform3DGroup>
<Transform3DGroup.Children>
<RotateTransform3D>
<RotateTransform3D.Rotation>
<AxisAngleRotation3D x:Name="myHorizontalRotation" Angle="0" Axis="0 1 0" />
</RotateTransform3D.Rotation>
</RotateTransform3D>
<RotateTransform3D>
<RotateTransform3D.Rotation>
<AxisAngleRotation3D x:Name="myVerticalRotation" Angle="0" Axis="1 0 0" />
</RotateTransform3D.Rotation>
</RotateTransform3D>
<RotateTransform3D>
<RotateTransform3D.Rotation>
<AxisAngleRotation3D x:Name="my3DRotation" Angle="0" Axis="0 0 1" />
</RotateTransform3D.Rotation>
</RotateTransform3D>
</Transform3DGroup.Children>
</Transform3DGroup>
</ModelVisual3D.Transform>
</ModelVisual3D>
</Viewport3D>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding IsSpin}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard Name="Roll2">
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="myVerticalRotation"
Storyboard.TargetProperty="Angle"
From="0" To="360" Duration="0:0:4"
RepeatBehavior="Forever" AutoReverse="True"/>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<StopStoryboard BeginStoryboardName="Roll2"/>
</DataTrigger.ExitActions>
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>`
但是它会抛出一个异常,说没有找到这个资源。我在我的视图模型中开始故事板是这样的:
v.AngleRotation.Begin();
我尝试将Application.Current.FindResource("UnderOverTemplate")
作为参数放入Begin
,但它说它没有找到资源。
在xaml.cs我有:
public Storyboard AngleRotation
{
get { return (Storyboard)Resources["AngleRotation"]; }
}
我尝试将绑定设置为RelativeResource
AncestorType
动画目标名称中的 ,但效果不佳。我怎样才能TargetName
正确绑定,以便它找到UnderOverTemplate
?