March 30, 2007
12:03 AM

I was just skimming the Petzold Book Blog and noticed the Animating WPF Resources entry. Petzold provides an interesting solution which allows us to assign an x:Name to resources.

While interesting, if something can be done in pure XAML I would generally prefer that to writing code. This time there is a XAML-only solution. In order reference a resource from a StoryBoard we really just need to provide some sort of entry point within the element tree. In this case the DataContext of the Polyline is available, were it not we could just as simply use the ever useful Tag property.

<Polyline Name="polyline" 
          DataContext="{StaticResource curler}" 
          Stroke="Blue"
          StrokeThickness="10"
          Points="{Binding Points}" />

Somewhat confusingly the Storyboard property TargetProperty is really a TargetPath. This lets us step into the Angle property of Curler using the DataContext of Polyline.

<Storyboard TargetName="polyline" TargetProperty="DataContext.Angle">
© Douglas Stockwell 2007
Creative Commons License Unless otherwise specified all "source code" examples are available for use under the Creative Commons Attribution-Noncommercial 3.0 License. Please contact me if you would like more flexible licensing terms.
Messenger Presence