1

我有一个这样定义StoryBoardDoubleAnimation内部: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>

我正在尝试将此动画绑定到 aModelVisual3DRotateTransform3D.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

4

0 回答 0