Silverlight – Change the storyboard TargetName

I wanted to reuse the same animation for multiple objects, which should have been a simple task.  Unfortunately, it took me a while to figure it out. 

In theory, all I should need to do is set the Storyboard.TargetName property to a different object and I am done.  I tried all kind of combinations and couldn’t get it to work.

Luckily, it’s a simple fix.  Do NOT add the TargetName in the XAML.  So your Storyboard might look like this:

<Storyboard x:Name=”sbMouseOver” >
  <ColorAnimation Storyboard.TargetProperty=”(Fill).(Color)”  To=”Red”  Duration=”0:0:00.1″/>

In your code, you can assign the storyboard to any object by, stopping it, setting the TargetName then starting it.  For example:

this.sbMouseOver.SetValue<string>(Storyboard.TargetNameProperty, Rectangle2.Name);