Question

For myself, It would be logic if MeasureOverride calculates the size of the FrameworkElement and ArrangeOverride just positions the child elements. But ArrangeOverride does not only arrange the child elements. It does also calculate the size of the FrameworkElement again which I do not understand. Why can't MeasureOverride calculate the final size and thats it?

Was it helpful?

Solution

Because your element is potentially not the only one on the screen.

Layout is not that simple. WPF has to work out the actual physical space it has to use, then calculate how much space each element wants, scale the requested amount if it can, then apply it. Additionally some elements may want to make changes based on the exact amount of space allocated.

This previous answer of mine gives it to you as an analogy.

OTHER TIPS

If you implement your own Panel, with your own layout algorithm and in your ArrangeOverride() call

child.Arrange(rect1)

with rect1 being different than the child's DesiredSize then the system may arbitrarily decide to ignore rect1 size and use something different. I've seen DesiredSize being used, I'm not sure if it's always the case. I'd say that this is something resembling a bug :)

If rect1 is calculated in the MeasureOverride() pass of the Panel then a workaround for this behavior is to call Measure() on the child for a second time passing size of rect1, so DesiredSize of the child is recalculated before the Arrange() pass.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top