title: How to retrieve Storyboard.Target if target was set using Storyboard.SetTargetName?
body: <p>The why: I am trying to implement an extension for Storyboard (or more generally TimelineGroup) that can be run after Storyboard.Completed to "unlock" the animated properties, and leaving them at their new value (quite a goofy workaround in itself, see <a href="https://stackoverflow.com/questions/942276/boolean-animation-locks-property">Boolean Animation locks property</a> for background).</p>
  
  <p>So the extension below provides a uniform way of doing this rather than trying to keep track of each property changed &amp; what the value was expected end up as.  </p>
  
  <pre><code>public static void ReleaseAnimatedProperties(this TimelineGroup storyboard) {
  
      foreach (Timeline anim in storyboard.Children) {
          // This is null unless Storyboard.SetTarget(anim, &lt;animatable&gt;) has been called
          Animatable animatable = (Animatable)Storyboard.GetTarget(anim);
  
          PropertyPath path = Storyboard.GetTargetProperty(anim);
          DependencyProperty dp = (DependencyProperty)path.PathParameters[0];
  
          animatable.SetValue(dp, animatable.GetValue(dp)); // Set it to what it ended up as
          animatable.ApplyAnimationClock(dp, null); // Drop the "locking" on the animated property
  
      }
  
  }
  </code></pre>
  
  <p>The what: How can I retrieve the Storyboard.Target?</p>
  
  <p>Storyboard.GetTarget returns null UNLESS Storyboard.SetTarget was used.  But when the target is not a UIElement (for instance when it is a ScaleTransform), SetTarget will cause the animation to fail.  Instead (and not in addition) Storyboard.SetTargetName must be used.  See this issue: <a href="https://stackoverflow.com/questions/2957582/wpf-storyboard-settarget-vs-storyboard-settargetname">Storyboard.SetTarget vs Storyboard.SetTargetName</a>.  So we've got a catch-22 - is there any other way to pull out the Storyboard.Target (and not just its name) from an animation?</p>
  
  <p>I'm aware I can tack on a bunch of anonymous methods as the animation is built, so that I know which object I'm dealing with, but this would be so much cleaner.</p>