-1

I am writing a custom panel and I would like to know how to tell my children that when they need to to remeasure their parent should also do the remeasuring.

As example one of the children changes its width and the parent should also remeasure again, leading his parent to also do the remeasuring and then the parent of his parent and the parent of his parent and so on.. Its like going up the VisualTree. How do I do that?

Here is code of measure of panel.. but how to tell to parent to also remeasure

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);
}
cyberist
  • 107
  • 1
  • 10
  • 1
    You don't do that. It's done by WPF automatically. Simply try it. – Clemens Feb 09 '13 at 20:58
  • well its not doing automatically.. if i tell the panel you have the size of 200 x 200 and then i change the width of a child to 300, the panel doesnt get updated.. which is my i am asking this question – cyberist Feb 09 '13 at 21:28
  • 1
    You need to provide more information about what you are trying to achieve and why and show some code. The people of Stackoverflow cannot offer help if we are left guessing. – Benjamin Gale Feb 09 '13 at 21:35
  • this is my post.. there is code http://stackoverflow.com/questions/14775226/measure-control-with-double-positiveinfinity-wpf – cyberist Feb 09 '13 at 21:39
  • 1
    If the information is the same then you should not post another question. If it is different and warrants another question then please take the time to provide the information here. Also, have you tried the suggestions given for the other question? – Benjamin Gale Feb 09 '13 at 21:48

1 Answers1

1

Take a look at this very simple custom Panel that arranges child elements in the upper left corner:

public class MyPanel : Panel
{
    protected override Size MeasureOverride(Size availableSize)
    {
        Trace.TraceInformation("MeasureOverride");

        var size = new Size();

        foreach (UIElement element in InternalChildren)
        {
            element.Measure(availableSize);

            size.Width = Math.Max(size.Width, element.DesiredSize.Width);
            size.Height = Math.Max(size.Height, element.DesiredSize.Height);
        }

        return size;
    }

    protected override Size ArrangeOverride(Size finalSize)
    {
        Trace.TraceInformation("ArrangeOverride");

        foreach (UIElement element in InternalChildren)
        {
            element.Arrange(new Rect(element.DesiredSize));
        }

        return finalSize;
    }
}

If you use it like shown below with a Button child

<local:MyPanel>
    <local:MyPanel>
        <Button Width="100" Height="100" Click="Button_Click"/>
    </local:MyPanel>
</local:MyPanel>

and a Button_Click handler that resizes the Button

private void Button_Click(object sender, RoutedEventArgs e)
{
    ((FrameworkElement)sender).Width += 20;
}

you will observe that on every button click the parent and grandparent panel will be measured and arranged. The trace output looks like this:

CustomPanelTest.vshost.exe Information: 0 : MeasureOverride
CustomPanelTest.vshost.exe Information: 0 : MeasureOverride
CustomPanelTest.vshost.exe Information: 0 : ArrangeOverride
CustomPanelTest.vshost.exe Information: 0 : ArrangeOverride

Hence there is no need to call Measure or Arrange manually on a parent Panel.

Clemens
  • 123,504
  • 12
  • 155
  • 268
  • I pass to my panel 200x200 and then that value gets passed to the children.. but when i resize a child the panel doesnt get update and remeasured so i guess its not working for me because i am using fix size values. what do i need to do to make it work – cyberist Feb 10 '13 at 05:00
  • You should never fix the size of a Panel when at the same time you want it to dynamically react on child element size changes. – Clemens Feb 10 '13 at 08:55
  • well what i ment it that i return 200x200 as the size in the measureoverride in panel but then once the width of any child is greater than 200 the panel doesnt get measured again – cyberist Feb 10 '13 at 10:03
  • 1
    You should perhaps post your MeasureOverride and ArrangeOverride code. – Clemens Feb 10 '13 at 10:31
  • 1
    Where is the 200x200 stuff? And your `ArrangeOverride` would also be interesting. – Clemens Feb 10 '13 at 18:40
  • availableSize is 200x200.. the window as the root element is 200x200 where else would it come from hehe – cyberist Feb 10 '13 at 20:52
  • Your sample code does not show that you return `availableSize`. In fact, it is ignored completely. Returning `availableSize` would be wrong anyway, since you should return the desired size of the Panel (as you correctly do in the sample). So what are we talking about here? You may read the documentation of [MeasureOverride](http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.measureoverride.aspx) again to get a picture. – Clemens Feb 10 '13 at 21:10