如何将Storyboard的TargetName绑定到同一资源字典中的控件模板的Transform属性?

时间:2017-09-07 08:22:18

标签: wpf mvvm resourcedictionary doubleanimation

我的StoryBoard DoubleAnimationResourceDictionary定义如下:

<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>`

AngleX1Duration1是视图模型中定义的属性。

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

0 个答案:

没有答案