c# - 如何在代码后面设置Storyboard.SetTargetProperty?

标签 c# .net wpf xaml

我正在尝试将 XAML Storyboard 转换为 Storyboard 背后的代码。但我遇到了一个异常(exception)

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

最佳答案

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

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

谢谢大家。

关于c# - 如何在代码后面设置Storyboard.SetTargetProperty?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9727133/

相关文章:

.net - Console.WriteLine() 与 Debug.WriteLine() 有什么区别?

c# - 如何从匿名委托(delegate)或 lambda 访问安全关键字段?

.net - ToUpperInvariant() – MSDN 的建议是否错误?

c# - 防止弹出窗口在单击 wpf 时关闭

c# - 如何将类方法的值返回到调用它的相应页面

c# - 将选定的下拉列表值从 View 传递到 Controller

c# - C#中的db.Database.SqlQuery()使用问题

c# - WPF 强制用户停留在 TabItem 上

c# - Microsoft.Office.Interop.Excel 的阅读范围

c# - 我如何获得光标 :pointer on an embedded object?