Skip to main content
Home Forums Silverlight Programming Report a Silverlight Bug Event model and initialization with custom controls
2 replies. Latest Post by jpsscott on October 9, 2008.
(0)
jpsscott
Member
51 points
84 Posts
10-07-2008 3:31 PM |
Hi,
I've worked on a few custom controls now and noticed a fairly annoying trend:
Lets say I have a control that renders its contents when I call a Load function.
When I call the controls Load function from the Load event of a user control the custom control is used in I will usually run into problems because it seems that OnApplyTemplate hasn't been called yet in the chain of events.
If a user control is loaded, why can't I access the functionality of my custom control? Am I not wiring something up right in the custom control?
Basically I have some markup in generic.xaml, set the DefaultStyleKey and then in OnApplyTemplate I set a few member variables to controls I use within the custom control. But OnApplyTemplate doesn't seem to be called until after my user control containing the control is loaded.
So now I've got code littered throughout my custom controls saying: if (someVar != null) {AdjustUI();}
And then I've got code in LayoutUpdated etc to do the same thing.
There has got to be a better way to do this.
Comments anyone?
James
Yi-Lun L...
All-Star
25052 points
2,747 Posts
10-09-2008 3:34 AM |
Unfortunately this is a limitation in Silverlight 2. Once we tried to fix the lifecycle problem but ran into a lot of browser related issues... Maybe the behavior will change in a future version, but I can't assure you anything at this time... A workaround for template is to call ApplyTemplate to force the template to be applied. But if you have problems related to layout, there's no good workarounds...
10-09-2008 7:23 AM |
OK, thanks.
I'm putting ApplyTemplate in my constructor and I'll see how that goes. Is there a better spot to put it?
The other thing I'm surprised about is I haven't seen this mentioned on any other examples...
Regardless, thanks for the help and the additional info!