locked
How to I apply an XAML declared animation to multiple controls dynamically? RRS feed

  • Question

  • Hello,

    How to I apply an XAML declared animation to multiple controls dynamically?

    Here is the XAML declared animation:

      <Window.Resources>
        <Storyboard x:Key="TextBlock_SB">
          <DoubleAnimation 
                Storyboard.TargetName="TextBlock_1"
                Storyboard.TargetProperty="(TextBlock.Opacity)"
                From="1.0"
                To="0.75"
                Duration="0:0:0.5"
                AutoReverse="True"
                RepeatBehavior="Forever" />
        </Storyboard>
    
    
    Here is the code that starts the animation for TextBlock_1:
            (this.Resources["TextBlock_SB"] as Storyboard).Begin();

    I want to apply the same storyboard/animation to TextBlock_2 and TextBlock_3 and perhaps others.  How do I do that without defining more XAML storyboards?

    Regards,

    Robert

     

    Sunday, August 29, 2010 8:03 PM

Answers

  • Hi R-F-M,

    Sorry for missing this. You need to explicitly set isControllable to true if you want to manually stop these StoryBoards later.

    Here is an example:

          (this.Resources["TextBlock_SB"] as Storyboard).Begin(TextBlock_2,true);
          (this.Resources["TextBlock_SB"] as Storyboard).Begin(TextBlock_3,true);
    

    If you still have any doubts or concerns about this issue, please feel free to let me know.

    Best regards,

    Min

     


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by R-F-M Friday, September 3, 2010 12:57 AM
    Thursday, September 2, 2010 1:35 AM

All replies

  • Hi Robert,

    This might help. Though it is a silverlight resource but should be applicable to WPF.

    http://blogs.msdn.com/b/silverlight_sdk/archive/2008/03/26/target-multiple-objects-properties-with-one-animation-silverlight.aspx

    You can also use StoryBoard.SetTargetName() to change the target name of the animation. Later when you would call BeginStoryBoard(), the animation should be applied to the new target.

    Thanks,
    Muhammad
    shujaatsiddiqi.blogspot.com

    Monday, August 30, 2010 11:23 AM
  • Hi Robert,

    Muhammad's solution should work.

    Alternatively, you can also remove Storyboard.TargetName from XAML, then you can use it on anything FrameworkElement via Storyboard.Begin.

    Here is an example:

      <Window.Resources>
        <Storyboard x:Key="TextBlock_SB">
          <DoubleAnimation 
          Storyboard.TargetProperty="(TextBlock.Opacity)"
          From="1.0"
          To="0.3"
          Duration="0:0:0.5"
          AutoReverse="True"
          RepeatBehavior="Forever" />
        </Storyboard>
      </Window.Resources>
    

    Code-behind:

          (this.Resources["TextBlock_SB"] as Storyboard).Begin(TextBlock_2);
          (this.Resources["TextBlock_SB"] as Storyboard).Begin(TextBlock_3);
    

    Hope this helps.

    Best regards,

    Min


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Wednesday, September 1, 2010 6:25 AM
  • Hello Min,

    Thanks for your input.

    That code does start the animations:

       (this.Resources["TextBlock_SB"] as Storyboard).Begin(TextBlock_2);
       (this.Resources["TextBlock_SB"] as Storyboard).Begin(TextBlock_3);
    
    
    But I can't stop the animation with:

       (this.Resources["TextBlock_SB"] as Storyboard).Stop(TextBlock_2);
       (this.Resources["TextBlock_SB"] as Storyboard).Stop(TextBlock_3);
    

    I need to be able to start and stop the animations.

    Regards,

    Robert

     

    Wednesday, September 1, 2010 9:19 PM
  • Hi R-F-M,

    Sorry for missing this. You need to explicitly set isControllable to true if you want to manually stop these StoryBoards later.

    Here is an example:

          (this.Resources["TextBlock_SB"] as Storyboard).Begin(TextBlock_2,true);
          (this.Resources["TextBlock_SB"] as Storyboard).Begin(TextBlock_3,true);
    

    If you still have any doubts or concerns about this issue, please feel free to let me know.

    Best regards,

    Min

     


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by R-F-M Friday, September 3, 2010 12:57 AM
    Thursday, September 2, 2010 1:35 AM
  • Hello Min,

    That did it, thanks a lot.

    Regards,

    Robert

    Friday, September 3, 2010 12:58 AM