views:

26

answers:

1

I'm trying to make and use templated controls in an application but I seem to run into timing isssues. When I use a custom templated control, in XAML <local:MyControl> and through code (new MyControl()), and call a method on the control that tries to do something with a control in the Controltemplate, a control in the template is almost always null even after the OnApplyTemplate method. One solution is to wait for the control to initialize, using an event thrown in the OnApplyTemplate method, and then let the code do something on the controls in the template but this seems to replicate control/framework features that I shouldn't have to worry about.

So the questions are:

Is the loading of the ControlTemplate from the Themes\Generic.xaml asynchronous? How do I use these multiple level templated controls in a MVVM context?

A: 

In OnApplyTemplate you need to use the GetTemplateChild method to get to the controls. See here for a description of the control lifecycle.

Alex Paven