Ok, let me first say that Dr. WPF is my hero. Not just because of his WPF snippet library. But especially because of his community involvement in the WPF forum.
Today, I was wracking my brain against a problem that I’ve never run into before and … and it was Dr. WPF to the rescue (via the forum).
I have a custom control that I was retemplating with a fancy, schmancy template. This ControlTemplate had a Resources section where I had a couple Storyboards which were targeting items in the visual tree of the fancy template. I was also launching this Storyboard from the code for the custom control.
However for some reason, it couldn’t resolve the Storyboard.TargetName(s). It kept coming back with the error: ‘<Storyboard.TargetName>’ name cannot be found in the name scope of ‘<Namespace.CustomControl>’.
(This is where the hair pulling happened.)
After some research: here, here, and here … I stumbled across Dr. WPF’s forum answer on the matter. That led me to look very closely at how I was calling the Begin on the Storyboard.
Here is the old and bad code:
And, here is the good code:
What did I do differently? Well, I had always thought that the argument to the Begin method above was an object containing the items that are being targeted by the Storyboard … and that is what the IntelliSense parameter tends to suggest (containingObject) …
However, looking closer at the documentation on the parameter it says: An object contained within the same name scope as the targets of this Storyboard’s animations. Ah, ha!
After that, all I had to do was grab one of the elements in the visual tree (_button in the above code snippet) and pass that to the Begin method (whereas before I was just passing the custom control instance in, the ‘this’ in the first code snippet above).
And all of this is due to a friendly answer by the good doctor.
I think there is another and better way to solve this problem. Check out this question/answer on StackOverflow. Bascially, there is an overload to the Begin method that lets you send in the control template. Nice. Don’t know how I missed that overload.