locked
Storyboard target RRS feed

  • Question

  • I have programmatically created a StoryBoard and applied it to a number of objects. In the StoryBoard Completed event I'd like to access the underlying object which I set using Storyboard.SetTarget(animation, control). I tried using StoryBoard.GetValue(Storyboard.TargetProperty) and it returns null. Ideas "
    Tuesday, June 23, 2009 8:50 AM

Answers

  • are you sure you have been given a name to the target

    coz If you create a new shape without name can hook it to the animation using

    • Storyboad.SetTarget(myAnimation, Target_item);  
    • here, Target_item doesnt have x:Name property, what will it return? 

     but not

    Target_item.SetValue(FrameworkElement.NameProperty, “mytarget”)
    Storyboard.SetTargetName(myAnimation, “mytarget”)

     here, let say you have set it before in xaml or as my work, you can sure get its name

    So result will be different

    I think prooks code should work

    Thursday, June 25, 2009 1:44 PM
  • SteveWong is right.  I wrote some test code that included the following:

    Storyboard.SetTargetName(FadeIn, control.Name);

    and Storyboard.GetTargetName(animation) returned the name as expected.

    EDIT: For completeness, here is the code that I used which works for me.

     

    public Storyboard AnimateFadeIn(FrameworkElement control, double fromOPacity, double toOpacity, double startOffsetSeconds, double durationSeconds)
    {
        DoubleAnimation FadeIn = new DoubleAnimation();  //Animation for X position
        FadeIn.From = fromOPacity;
        FadeIn.To = toOpacity;
    
        Duration duration = new Duration(new TimeSpan(0, 0, 0, (int)(durationSeconds)));
        //duration = new Duration(new TimeSpan(0, 0, 0, (int)durationSeconds));
    
        Storyboard sb = new Storyboard();
        sb.Completed += sb_Completed;
        sb.Duration = duration;
        sb.Children.Add(FadeIn);
        //sb.BeginTime = new TimeSpan(0, 0, 0, 0,(int)(startOffsetSeconds * 1000));
    
        Storyboard.SetTargetName(FadeIn, control.Name);
        Storyboard.SetTarget(FadeIn, control);   //set Animation Target
        Storyboard.SetTargetProperty(FadeIn, new PropertyPath("(Rectangle.Opacity)"));
        sb.Begin(); // Start to run the Storyboard
    
        return sb;
    }
    
    void sb_Completed(object sender, EventArgs e)
    {
        var storyboard = sender as Storyboard;
        var animation = storyboard.Children[0] as DoubleAnimation;
        var target = Storyboard.GetTargetName(animation);
        var elementReference = LayoutRoot.Children.Single(c => ((FrameworkElement)c).Name == target);
    } 
     
    Thursday, June 25, 2009 2:04 PM

All replies

  • Try using: Storyboard.GetTargetName(animation)

    It should return the name of the target.  If you wanted to get an actual reference to the element, you could do the following:

    var target = Storyboard.GetTargetName(Da1);
    var elementReference = LayoutRoot.Children.Single(c => ((FrameworkElement) c).Name == target);

    There is probably a better way to do this, but this is one possibility

     

    Wednesday, June 24, 2009 12:24 PM
  • Thanks..

    I have a static method that sets up a fade-in/fade-out on any element below. This method returns the StoryBoard.

    In the calling method I have set up an event when the storyboard completed (StoryBoard.Completed).

    In the completed event (which gets the Storyboard handed over) , I would like to get  to the uiElement the storyboard is applied to. At that point I dont have acccess to the animation object.

     

     

            public static Storyboard AnimateFadeIn(FrameworkElement control, double fromOPacity, double toOpacity, double startOffsetSeconds, double durationSeconds)
            {
                DoubleAnimation FadeIn = new DoubleAnimation();  //Animation for X position
                FadeIn.From = fromOPacity;
                FadeIn.To = toOpacity;

                Duration duration = new Duration(new TimeSpan(0, 0, 0, (int)(durationSeconds * 1000.0)));
                //duration = new Duration(new TimeSpan(0, 0, 0, (int)durationSeconds));

                Storyboard sb = new Storyboard();
                sb.Duration = duration;
                sb.Children.Add(FadeIn);
                //sb.BeginTime = new TimeSpan(0, 0, 0, 0,(int)(startOffsetSeconds * 1000));

                Storyboard.SetTarget(FadeIn, control);   //set Animation Target
                Storyboard.SetTargetProperty(FadeIn, new PropertyPath("(Target_item.Opacity)"));
                sb.Begin(); // Start to run the Storyboard

                return sb;
            }

    Thursday, June 25, 2009 9:25 AM
  • What if you did something like this?  I haven't actually tested this code, but I think it might give you what you need.

    In your AnimateFadeIn method:

    ...
    ...
    Storyboard sb = new Storyboard();
    sb.Completed += new EventHandler(sb_Completed);
    ...
    ...

    static void sb_Completed(object sender, EventArgs e)
    {
        var storyboard = sender as Storyboard;
        var animation = storyboard.Children[0] as DoubleAnimation;
        var target = Storyboard.GetTargetName(animation);
        var elementReference = LayoutRoot.Children.Single(c => ((FrameworkElement)c).Name == target);
    }

    Thursday, June 25, 2009 9:42 AM
  •  Thanks again for the quick responses..      

    var target = Storyboard.GetTargetName(animation); returns null ( i.e. target is null)

    Thursday, June 25, 2009 9:56 AM
  • are you sure you have been given a name to the target

    coz If you create a new shape without name can hook it to the animation using

    • Storyboad.SetTarget(myAnimation, Target_item);  
    • here, Target_item doesnt have x:Name property, what will it return? 

     but not

    Target_item.SetValue(FrameworkElement.NameProperty, “mytarget”)
    Storyboard.SetTargetName(myAnimation, “mytarget”)

     here, let say you have set it before in xaml or as my work, you can sure get its name

    So result will be different

    I think prooks code should work

    Thursday, June 25, 2009 1:44 PM
  • SteveWong is right.  I wrote some test code that included the following:

    Storyboard.SetTargetName(FadeIn, control.Name);

    and Storyboard.GetTargetName(animation) returned the name as expected.

    EDIT: For completeness, here is the code that I used which works for me.

     

    public Storyboard AnimateFadeIn(FrameworkElement control, double fromOPacity, double toOpacity, double startOffsetSeconds, double durationSeconds)
    {
        DoubleAnimation FadeIn = new DoubleAnimation();  //Animation for X position
        FadeIn.From = fromOPacity;
        FadeIn.To = toOpacity;
    
        Duration duration = new Duration(new TimeSpan(0, 0, 0, (int)(durationSeconds)));
        //duration = new Duration(new TimeSpan(0, 0, 0, (int)durationSeconds));
    
        Storyboard sb = new Storyboard();
        sb.Completed += sb_Completed;
        sb.Duration = duration;
        sb.Children.Add(FadeIn);
        //sb.BeginTime = new TimeSpan(0, 0, 0, 0,(int)(startOffsetSeconds * 1000));
    
        Storyboard.SetTargetName(FadeIn, control.Name);
        Storyboard.SetTarget(FadeIn, control);   //set Animation Target
        Storyboard.SetTargetProperty(FadeIn, new PropertyPath("(Rectangle.Opacity)"));
        sb.Begin(); // Start to run the Storyboard
    
        return sb;
    }
    
    void sb_Completed(object sender, EventArgs e)
    {
        var storyboard = sender as Storyboard;
        var animation = storyboard.Children[0] as DoubleAnimation;
        var target = Storyboard.GetTargetName(animation);
        var elementReference = LayoutRoot.Children.Single(c => ((FrameworkElement)c).Name == target);
    } 
     
    Thursday, June 25, 2009 2:04 PM