What is the correct place to execute code that requires the ActualWidth property.
views:
31answers:
1
+1
A:
Short answer assuming your question relates to the control's lifecycle:
When it is rendered.
As you may have noticed, the Actual(Width|Height)
properties are set to zero when accessing them before the control is loaded.
Andreas
2010-07-05 07:33:02
yes, but when is the 'when it is shown'? Should I override OnRenderSizeChanged or is there another method where I can be certain that the ActualWidth has been updated?
Patrick Klug
2010-07-05 07:35:38
I would slightly correct that to *when it is rendered*, since it doesn't have to be rendered on-screen for the property to be set, but close enough :)
Dan Puzey
2010-07-05 07:49:08
Whenever the `Render()` method is called, you can access the `ActualWidth` property.
decyclone
2010-07-05 07:53:18
@Patrick: You could use the SizeChanged event handler.@Dan: totally agree, best indicator for this is that it is available in a Loaded event handler.
Andreas
2010-07-05 08:13:12
@Andreas: It doesn't seem to be available in the Loaded event handler, at least when I put a control in a tabcontrol it is Loaded before it is displayed and therefore the ActualWidth and ActualHeight are 0. The SizeChanged seems to work. I just wanted to know if there is one 'correct' place where to access ActualWidth and ActualHeight outside of the OnRender method.
Patrick Klug
2010-07-06 04:04:29
@Patrick: Then I think that the loaded event is fired completely asynchronously while the control is rendered. I will further examine this because it seems to be a little bit undeterministic to me by now.
Andreas
2010-07-06 06:50:00