如何在Code中设置Storyboard.SetTargetProperty?

时间:2012-03-15 19:51:57

标签: c# .net wpf xaml

我正在尝试将XAML Storyboard转换为Storyboard背后的代码。但我得到了一个例外

Cannot resolve all property references in the property path
  '(UIElement.RenderTransform).(myTransformGroup.Children)[1].(TranslateTransform.Y)'.
  Verify that applicable objects support the properties.  

我搜索了几个网站,但他们也是这样做的。我不明白我哪里错了。

XAML:

<Storyboard x:Key="Storyboard1">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" Storyboard.TargetName="r1">
            <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
            <EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="0.828"/>
            <EasingDoubleKeyFrame KeyTime="0:0:1" Value="0.828"/>
        </DoubleAnimationUsingKeyFrames>

        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" Storyboard.TargetName="r1">
            <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
            <EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="8.621"/>
            <EasingDoubleKeyFrame KeyTime="0:0:1" Value="8.621"/>
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>

C#代码:

  public void myAnimation(Rectangle myRectangle)
    {
        this.UnregisterName(myRectangle.Name);
        ScaleTransform myScaleTransform = new ScaleTransform();
        TranslateTransform myTranslateTransform = new TranslateTransform();
        TransformGroup myTransformGroup = new TransformGroup();
        myTransformGroup.Children.Add(myScaleTransform);
        myTransformGroup.Children.Add(myTranslateTransform);
        myRectangle.RenderTransform = myTransformGroup;
        this.RegisterName(myRectangle.Name, myTransformGroup);

        DoubleAnimationUsingKeyFrames myDoubleAnimationUsingKeyFrames = new DoubleAnimationUsingKeyFrames();
        EasingDoubleKeyFrame myEasingDoubleKeyFrame = new EasingDoubleKeyFrame();
        Storyboard myStoryboard = new Storyboard();

        Storyboard.SetTargetName(myDoubleAnimationUsingKeyFrames, myRectangle.Name);
        Storyboard.SetTargetProperty(myDoubleAnimationUsingKeyFrames, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"));

        myEasingDoubleKeyFrame.KeyTime = KeyTime.FromPercent(0);
        myEasingDoubleKeyFrame.Value = 1;
        myDoubleAnimationUsingKeyFrames.KeyFrames.Add(myEasingDoubleKeyFrame);

        myEasingDoubleKeyFrame.KeyTime = KeyTime.FromPercent(0.4);
        myEasingDoubleKeyFrame.Value = 0.828;
        myDoubleAnimationUsingKeyFrames.KeyFrames.Add(myEasingDoubleKeyFrame);

        myEasingDoubleKeyFrame.KeyTime = KeyTime.FromPercent(1);
        myEasingDoubleKeyFrame.Value = 0.828;
        myDoubleAnimationUsingKeyFrames.KeyFrames.Add(myEasingDoubleKeyFrame);

        myStoryboard.Children.Add(myDoubleAnimationUsingKeyFrames);

        Storyboard.SetTargetName(myDoubleAnimationUsingKeyFrames, myRectangle.Name);
        Storyboard.SetTargetProperty(myDoubleAnimationUsingKeyFrames, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[1].(TranslateTransform.X)"));

        myEasingDoubleKeyFrame.KeyTime = KeyTime.FromPercent(0);
        myEasingDoubleKeyFrame.Value = 0;
        myDoubleAnimationUsingKeyFrames.KeyFrames.Add(myEasingDoubleKeyFrame);

        myEasingDoubleKeyFrame.KeyTime = KeyTime.FromPercent(0.4);
        myEasingDoubleKeyFrame.Value = -17.5;
        myDoubleAnimationUsingKeyFrames.KeyFrames.Add(myEasingDoubleKeyFrame);

        myEasingDoubleKeyFrame.KeyTime = KeyTime.FromPercent(1);
        myEasingDoubleKeyFrame.Value = 165.5;
        myDoubleAnimationUsingKeyFrames.KeyFrames.Add(myEasingDoubleKeyFrame);

        myStoryboard.Children.Add(myDoubleAnimationUsingKeyFrames);

        myStoryboard.Begin(myRectangle);

    }

我猜问题出在 Storyboard.SetTargetProperty

2 个答案:

答案 0 :(得分:4)

我用这条线解决了我的问题。

new PropertyPath("RenderTransform.Children[0].ScaleY")

谢谢大家。

答案 1 :(得分:0)

尝试这样的PropertyPath

new PropertyPath("(0).(1)[0].(2)", UIElement.RenderTransformProperty, TransformGroup.ChildrenProperty, ScaleTransform.ScaleXProperty)

Further reading