How to set Storyboard.SetTargetProperty in Code behind?

10,385

I solve my problem using this line.

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

thank`s everyone.

Share:
10,385
Hasanuzzaman
Author by

Hasanuzzaman

A full stack software engineer with 7 years of experience in building large-scale applications. Pivotal contributions to the architecture and development life cycle of company projects. I am eager to use my technical skills and vast experiences in building .NET base application at your company.

Updated on June 04, 2022

Comments

  • Hasanuzzaman
    Hasanuzzaman almost 2 years

    I am trying to convert XAML Storyboard to code behind Storyboard. But I get an exception

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

    I searched several websites but they also do this the same way. I don`t understand where I am going wrong.

    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# Code:

      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);
    
        }
    

    I guess the problem is in Storyboard.SetTargetProperty

  • Hasanuzzaman
    Hasanuzzaman about 12 years
    I get another exception " '[Unknown]' property does not point to a DependencyObject in path '(0).(1)[0].(2)'."