1

I'm trying to create a storyboard completely in C#, no XAML at all but I'm having trouble with (SetTarget and SetTargetProperty) .. here's my code

I'm just animating my UserControl when the user navigates to it, it goes from 0 opacity to 100 and from 900 TransformX to 0 in .5 seconds.

I would really appreciate any help with setting these two parameters, been at it all day with no luck!

    public void Designer()
    {
        Control_ = new UserControl();

        Control_.HorizontalAlignment = HorizontalAlignment.Stretch;
        Control_.VerticalAlignment = VerticalAlignment.Stretch;

        Control_.Name = "Control_";
        this.AddChild(Control_);

        CreateStoryboard();
    }

    public void CreateStoryboard()
    {
        fadeinBoard = new Storyboard();
        Duration duration = new Duration(TimeSpan.FromMilliseconds(5));
        fadeinBoard.Duration = duration;

        DoubleAnimationUsingKeyFrames animOpacity = new DoubleAnimationUsingKeyFrames();
        DoubleAnimationUsingKeyFrames animTransform = new DoubleAnimationUsingKeyFrames();

        animOpacity.Duration = duration;
        animTransform.Duration = duration;

        //Transform Function
        KeyTime ktime1 = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(.5));
        PowerEase pow = new PowerEase();
        pow.Power = 5;
        pow.EasingMode = EasingMode.EaseOut;
        EasingDoubleKeyFrame keyFrame1 = new EasingDoubleKeyFrame(0, ktime1, pow);

        //Opacity Function
        KeyTime ktime2 = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(.5));
        ExponentialEase expo = new ExponentialEase();
        expo.Exponent = 3;
        expo.EasingMode = EasingMode.EaseOut;
        EasingDoubleKeyFrame keyFrame2 = new EasingDoubleKeyFrame(1, ktime2, expo);

        animOpacity.KeyFrames.Add(keyFrame1);
        animTransform.KeyFrames.Add(keyFrame2);

        // MY PROBLEM IS HERE
        Storyboard.SetTarget(???, ???);
        Storyboard.SetTarget(???, ???);
        Storyboard.SetTargetProperty(???, ???);
        Storyboard.SetTargetProperty(???, ???)));

        fadeinBoard.Children.Add(animOpacity);
        fadeinBoard.Children.Add(animTransform);

        Control_.Resources.Add("fader", fadeinBoard);
    }
hashguard
  • 403
  • 4
  • 22
  • Shouldn't it be : // MY PROBLEM IS HERE HERE Storyboard.SetTarget(animOpacity, Control_); Storyboard.SetTarget(animTransform, Control_); Storyboard.SetTargetProperty(animOpacity, new PropertyPath("OpacityProperty")); Storyboard.SetTargetProperty(animTransform, new PropertyPath(("RenderTransform.Children[0].TransformX"))); ? – Ben Jackson Jun 08 '16 at 14:19
  • @BenJackson neither works Ben, with or without the quotes I keep getting exceptions on that whole section. I just don't know how to set that value! – hashguard Jun 08 '16 at 14:28
  • @BenJackson I modified my question, hopefully this will make things clearer , sorry about before – hashguard Jun 08 '16 at 14:31
  • Does this solves your problem? http://stackoverflow.com/questions/910488/wpf-storyboard-beginner-problem – Rosdi Kasim Jun 09 '16 at 16:46
  • @RosdiKasim thank you rosdi for the help, but the code provided was XAML And I'm looking to do it in C# BenJackson actually helped resolve my problem and it's working just fine now – hashguard Jun 12 '16 at 08:04

1 Answers1

3

This is working for me with the following :

    Storyboard.SetTarget(animOpacity, Control_);
    Storyboard.SetTarget(animTransform, Control_);
    Storyboard.SetTargetProperty(animOpacity, new PropertyPath("Opacity"));
    Storyboard.SetTargetProperty(animTransform, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"));

Note the correction from OpacityProperty to Opacity, and the fully qualified property syntax - I've configured this to work on ScaleY for the moment.

This will only work though if you have added a RenderTransform to the control that can be accessed using the supplied path, e.g. as follows :

    ScaleTransform myScaleTransform = new ScaleTransform();
    myScaleTransform.ScaleY = 1;
    myScaleTransform.ScaleX = 1;
    TransformGroup myTransformGroup = new TransformGroup();
    myTransformGroup.Children.Add(myScaleTransform);
    Control_.RenderTransform = myTransformGroup;  
Ben Jackson
  • 1,108
  • 6
  • 9
  • It's impossible to know for sure what all is wrong in the code without a good [mcve] provided by the author of the question. However, the above seems to me to address the most glaring problem: the wrong value was being passed as the `DependencyObject` parameter for the `SetTargetProperty()` method calls. FWIW: it's my preference to pass the actual `DependencyProperty` value to the `PropertyPath` constructor. Fewer "magic literals" in the code that way. E.g. `Storyboard.SetTargetProperty(Control_, new PropertyPath(UIElement.OpacityProperty));` – Peter Duniho Jun 08 '16 at 21:52
  • Thank you so much @BenJackson that helped a lot It's working like you posted, now I just gotta figure out how to target the Translate property. But the storyboard is finally animating – hashguard Jun 09 '16 at 11:23
  • @PeterDuniho i'm sorry the question wasn't clear enough, I will try and provide more detail in the future. Thanks for the help – hashguard Jun 09 '16 at 11:25