I am working on a custom control that has a custom panel and inside the custom panel I have a small and simple MeasureOverride
method that passes the size of double.PositiveInfinity
to its children MeasureOverride
method. The custom panel should take care of the layout and it should make children bigger or smaller depending on window size.
If you have dealt with controls you should then know how wpf layout system works and that basically every child calls MeasureOverride which calls MeasureOverride of childs children and so on.
Now the problem is that when I resize the window, the custom panel does get receive the flag to do the measure again hence the MeasureOverride
does get called again but this time while passing the double.PositiveInfitinty
size to its children, the children MeasureOverride
doesn't get called at all (but the method should be called according to the definition of WPF layout system). Why is that so? I always thought when I call the MeasureOverride
on a parent that it children will also be forced to do the measure.
Obviously I am wrong so could somebody explain me how does a control/child know when to measure again?
By the way I am passing the size of double.PositiveInfinity
to the children to tell them to take as much space as needed.
Code:
protected override Size MeasureOverride(Size availableSize)
{
double x;
double y;
var children = this.InternalChildren;
for (int i = 0; i < children.Count; i++)
{
UIElement child = children[i];
child.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity);
y += child.DesiredSize.Height;
x = Math.Max(x, child.DesiredSize.Width);
}
return new Size(x, y);
}
Code is simple. I dont get it why the children doesnt get measured again. And sorry if i have misspelled something in code.
Your children won't get measured again because they don't have to: since you keep passing the same size, WPF knows that there is no reason to measure again since the resulting
DesiredSize
will be the same.That being said, the
Desiredsize
updated byMeasure
is only the size that your child considers the most appropriate. You should take it into account when overridingArrangeOverride
, but you have no obligation to: simply arrange your children the way you like, the fact thatMeasureOverride
is called or not for your children should be irrelevant.Have you tried calling the
UIElement.InvalidateMeasure
whereUIElement
is your parent panel?