我的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>
我尝试将此动画绑定到ModelVisual3D
的{{1}}属性,RotateTransform3D.Rotation
包含在名为ModelVisual3D
的{&#}中34; UnderOverTemplate&#34;,它包含在同一个ControlTemplate
:
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")
中,但它说它没有找到资源。
在 xaml.cs 中我有:
Begin
我尝试将绑定设置为动画目标名称中的public Storyboard AngleRotation
{
get { return (Storyboard)Resources["AngleRotation"]; }
}
RelativeResource
,但它也不起作用。
如何正确绑定AncestorType
以便找到TargetName
?