3
votes

I have a custom control that is "hosted" in a custom panel (both mine).

My custom panel has code like this:

protected override Size MeasureOverride(Size availableSize)
{
  foreach (UIElement child in Children)
  {
     child.Measure(availableSize);

     // Do stuff with the Desired Size.  (This is an example)
     resultSize.Width += child.DesiredSize.Width;
     resultSize.Height = Math.Max(resultSize.Height, child.DesiredSize.Height);

  }
  //.. Other Measure Stuff
}

All the standard controls I put in my panel work fine. But my custom control has DesiredSize set to a very small width (5 pixels). Even though it has a Label on it that wants at least 40 pixels to be able to show.

So I put this code into my custom control:

protected override Size MeasureOverride(Size constraint)
{
    var baseSize = base.MeasureOverride(constraint);
    var labelTextWidth = GetStringWidth(label.Content.ToString(), label);

    if (baseSize.Width == 0)
        baseSize.Width = 100;

    if (baseSize.Width < labelTextWidth)
        baseSize.Width = labelTextWidth;

    return baseSize;
}

This returns a Size that is correct. But in my Panel's MeasureOverride, the child.DesiredSize does not reflect what I returned from my child control's MeasureOverride.

Any ideas why it would do that? And what I can do to get it to pass my calculated Measure to the panel correctly in the DesiredSize? (you can't just set DesiredSize.)

1
Please post the template of your custom control and also what you are getting as the availableSize in the measureoverride of your panel. - AndrewS
@AndrewS - The template of my custom panel is too large to post here (255 lines). I loaded it as a gist here: gist.github.com/Vaccano/5173613 . availableSize is {510, Infinity} - Vaccano

1 Answers

0
votes

If MeasureOverride of custom control return correct size, but it is not reflected in DesiredSize property, I have only one suggestions. Your child element is wrapped by some container, you can check type of child element inside MeasureOverride procedure of custom panel.