My question here is more of a theoretical one rather than functional. So what I'm looking for is an answer that can maybe be backed up with some documentation.
I find myself in the situation where I need to do some custom measurements/arrangements for a wpf control's children?
Some pseudocode of what I'm trying to achieve:
MeasureOverride(size)
{
foreach(child in children)
{
if(condition)
{
child.measure(size)
}
else
{
child.measure(customSize)
if(...)
{
...
}
...
}
}
}
Should all the logic for this live inside MeasureOverride or should I just call measure with the size received as a parameter on all children and then do the logic inside ArrangeOverride? Thanks!
I think you can use any size. The argument to the MeasureOverride method specifies the size available for the control. the control can than return whatever DesiredSize it needs, even if it's bigger than the AvailableSize.
Child controls will do the same. the result of calling Measure on a child is that that child's DesiredSize is set. Again this can be anything, depending on the child's measuring logic. In your MeasureOverride method you can use your logic to make sense of the returned DesiredSizes.
If you have measurement logic it should definitely live in the MeasureOverride method (or helper methods) and not in ArrangeOverride. ArrangeOverride is for positioning only.
i'm sure you already checked this but i'll still post the link. http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.measureoverride%28v=vs.110%29.aspx
Some additional points:
When you do your size calculations, be careful not to create negative values. This can easily happen when DesiredSize is bigger than available size. Handle it like this:
double remainingWidth = Math.Min(0, availabeSize.Width - DesiredSize.Width);
You probably need to repeat the logic form MeasureOverride() in ArrangeOverride(). Careful when using values calculated in MeasureOverride() within ArrangeOverride(), because the available size is often different.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With