views:

172

answers:

2

I have a UserControl with a dependency property called ItemsSource. When the property is changed, I need to calculate the size of various elements. To do this, I have to access the UserControl's ActualWidth and ActualHeight properties.

The problem is, if an object is assigned to ItemsSource before a layout update has occurred, ActualWidth and ActualHeight are 0.

How can I ensure that a layout update occurs before responding to the property changed event while still allowing the property to be changed before a layout update has occurred?

+1  A: 

Is this what you are looking for - UpdateLayout()

This is a function which is defined on UIElement, however frequent use is not recommended as it will cause a layout pass.

Simon Fox
That doesn't seem to have any effect. ActualWidth and ActualHeight are still 0.
David Brown
A: 

I realized later that I don't actually need to rebuild the rows and columns every time my data source changes. Instead, I handle the SizeChanged event on my UserControl and perform the size calculations there. The event appears to fire when the control is first displayed, so it has a chance to make the initial calculations without any extra effort.

David Brown