将Usercontrol中的Storyboard绑定到Viewmodel中的Storyboard对象

时间:2017-09-19 13:06:27

标签: c# wpf storyboard

在提出这个问题之前,我已经搜索过了,但我的情况有所不同。 我在UserControl中有一个名为Storyboard的内容:

<Storyboard x:Key="Storyboard1" >
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)" Storyboard.TargetName="rectangle">
                <EasingDoubleKeyFrame KeyTime="0:0:2" Value="180"/>
                <EasingDoubleKeyFrame KeyTime="0:0:4" Value="360"/>
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" Storyboard.TargetName="smartphone_png">
                <SplineDoubleKeyFrame KeyTime="0:0:1.6" Value="205.5"/>
                <SplineDoubleKeyFrame KeyTime="0:0:2.2" Value="205.5"/>
                <EasingDoubleKeyFrame KeyTime="0:0:4" Value="466.667"/>
                <EasingDoubleKeyFrame KeyTime="0:0:5" Value="466.667"/>
                <EasingDoubleKeyFrame KeyTime="0:0:6" Value="632.334"/>
                <EasingDoubleKeyFrame KeyTime="0:0:7" Value="706.001"/>
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" Storyboard.TargetName="smartphone_png">
                <EasingDoubleKeyFrame KeyTime="0:0:6" Value="0"/>
                <EasingDoubleKeyFrame KeyTime="0:0:7" Value="53.333"/>
                <EasingDoubleKeyFrame KeyTime="0:0:8" Value="196.166"/>
            </DoubleAnimationUsingKeyFrames>
</Storyboard>

在ViewModel中,对于这个Usercontrol,我有类似的东西来绑定到Storyboard1 Storyboard:

Storyboard BoardControl = new Storyboard();

var resource =?? \\this line is what I need to bind to Storyboard1

BoardControl = resource as Storyboard;
BoardControl.Stop();

我试过了:

  

object resource = Application.Current.TryFindResource(&#34; Storyboard1&#34;);

这是错误的,因为它似乎初始化了一个新的Storyboard1对象,它与开头的对象不同。

对我而言,提前谢谢。

1 个答案:

答案 0 :(得分:0)

您无法控制或访问视图模型类中的Storyboard

如果需要以编程方式控制Storyboard,则应从视图的代码隐藏中执行此操作。然后,您可以像这样访问Storyboard资源:

Storyboard sb = this.Resources["Storyboard1"] as Storyboard;