Monday, 10 August 2009

Silverlight: Accessing child properties after they have been rendered

I came across an interesting issue today. Essentially its to do with the events related to a Silverlight User Control.

The problem I had was related to a child control which I wanted to set in its parent after the control had been rendered. For example setting a width on a child canvas control. The issue I had was when I wanted to set the parameter it had not yet been created, as loaded event is fired on the parent before the child.

The initialisation cycle of a Silverlight control (source Control Lifecycle), states the last event to be called is the ‘LayoutUpdated’ event. This seems to be fired when the child control has finished being rendered. Interesting!

   1: private void UserControl_LayoutUpdated(object sender, EventArgs e)
   2: {
   3:     //All controls are hopefully loaded set the template
   4:     ChildControl.ChildProperty.Width = <ChildValue>;
   5: }

No comments: