I have a StoryBoard
with a DoubleAnimation
inside a ResourceDictionary
defined like this:
<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
and Duration1
are properties defined in the view model.
The ResourceDictionary
is included in the view like this:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Resources/GamesResource.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
I'm trying to bind this animation to a ModelVisual3D
's RotateTransform3D.Rotation
property, the ModelVisual3D
is contained inside a ControlTemplate
named "UnderOverTemplate", which is contained inside the same 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>`
But it throws an exception saying that this resource was not found. I begin the storyboard in my view model like this:
v.AngleRotation.Begin();
I tried putting Application.Current.FindResource("UnderOverTemplate")
as an argument in Begin
, but it said it didn't find the resource.
In xaml.cs I have:
public Storyboard AngleRotation
{
get { return (Storyboard)Resources["AngleRotation"]; }
}
I tried setting the binding to the RelativeResource
AncestorType
in the animation target name, but it didn't work as well.
How can I bind the TargetName
properly so that it finds the UnderOverTemplate
?