Search code examples
silverlightmeasureoverride

How do you use MeasureOverride in Silverlight?


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 :-)


Solution

  • 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?