0

I think I have understood how MeasureOverrride works, but I am trying to use it in a very simple case and It doesn't work... So now I'm not so sure... After using Measureoverride do I have to use Arrageoverride too or the system will do it for me? The situation is this one: I have a LinearLayout class inherited from Panel and it has two fields called wrapwidht and wrapheigh, if they are true the width or the height of the LinearLayout has to be as its children require. so my Measureoveride looks like:

protected override Size MeasureOverride(Size availableSize) {

    Size panelDesiredSize = new Size();


    if ((this.widthWrap) || (this.heightWrap))
    {

        foreach (UIElement elemento in this.Children)
        {

            System.Diagnostics.Debug.WriteLine(((FrameworkElement)elemento).DesiredSize.ToString());

            if (this.Orientation.Equals(System.Windows.Controls.Orientation.Vertical))
            {
                if (this.widthWrap)
                {
                    //the widest element will determine containers width
                    if (panelDesiredSize.Width < ((FrameworkElement)elemento).Width)
                        panelDesiredSize.Width = ((FrameworkElement)elemento).Width;
                }
                //the height of the Layout is determine by the sum of all the elment that it cointains
                if (this.heightWrap)
                    panelDesiredSize.Height += ((FrameworkElement)elemento).Height;
            }
            else
            {
                if (this.heightWrap)
                {
                    //The highest will determine the height of the Layout
                    if (panelDesiredSize.Height < ((FrameworkElement)elemento).Height)
                        panelDesiredSize.Height = ((FrameworkElement)elemento).Height;
                }

                //The width of the container is the sum of all the elements widths
                if (this.widthWrap)
                    panelDesiredSize.Width += ((FrameworkElement)elemento).Width;
            }

        }
    }

    System.Diagnostics.Debug.WriteLine("desiredsizzeeeeeee" + panelDesiredSize);

    return panelDesiredSize;

}

The children I am aading to the LinerLayout are 3 buttons, but nothing is drawn.. even if the panelDesiredSize filed is correct.. so maybe I didn't understand how it works very well. If anybody can help me would be very nice :-)

slugster
  • 49,403
  • 14
  • 95
  • 145
Adriana
  • 225
  • 3
  • 8

3 Answers3

2

Check my answer on a previous post similar to yours: Two Pass Layout system in WPF and Silverlight

The answer is that no, you don't have to override ArrangeOverride, but what is the point of using MeasureOverride if you are not going to use ArrangeOverride?

Community
  • 1
  • 1
slugster
  • 49,403
  • 14
  • 95
  • 145
  • You are right, but I write the same code in ArrangeOverride and nothing happens neither.. IT is suppose to set the size of the current element and call the children arrange and add add the current element to the tree? this method is called by the parent of the current element, no? when the layouting process? Sorry, I ma reading a lot but nobody says who is calling this method... and what is the result going...... – Adriana Apr 14 '11 at 13:27
0

You must call Measure on "elemento". It's during the Measure that Silverlight creates the UI elements declared in the elemento's template since they'll be needed to actually measure and come up with a desired size. You should then use the elemento.DesiredSize.Width and Height to come up with the desired size for your panelDesiredSize.

0

You should call the Measure method on the child. See the example here: http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.measureoverride.aspx