I need to calculate the Width of each UIElement in a custom panel. If looping over Children:
foreach (UIElement child in Children) { ... }
I can't do child.DesiredSize.Width = myCalculatedWidth; since DesiredSize and DesiredSize.Width are read only.
The docs say that that Measure() updates the DesiredSize of the UIElement, so I tried:
Size size = new Size(myCalculatedWidth, Double.PositiveInfinity);
child.Measure(size);
But child.DesiredSize.Width is unchanged. How do I set the DesiredSize.Width?
If you call
Measuredirectly there are nothing to do for WPF layout system (because it caches the state of controls and store it as flags likeMeasureIsValid). You should callInvalidateMeasure()for this control (which will mark control asMeasureIsValid = false)andUpdateLayout()then to force layout to be updated.