开发者

MeasureOverride method doesn't set the desiredSize of the Panel

开发者 https://www.devze.com 2023-02-25 06:26 出处:网络
I have created a new Panel called LinearLayout, and Managing the Layouting I am having problem because the field desiredSize is not being set after the MeasureOverride method is called... here is my c

I have created a new Panel called LinearLayout, and Managing the Layouting I am having problem because the field desiredSize is not being set after the MeasureOverride method is called... here is my code:

    protected ove开发者_如何学Gorride Size MeasureOverride(Size availableSize)
    {
        Size panelDesiredSize = new Size();
        if ((this.widthWrap) || (this.heightWrap))
        {
            foreach (UIElement elemento in this.Children)
            {
                System.Diagnostics.Debug.WriteLine("Measure" + ((FrameworkElement)elemento).Name);
                ((FrameworkElement)elemento).Measure(new Size(((FrameworkElement)elemento).Width, ((FrameworkElement)elemento).Height));
                System.Diagnostics.Debug.WriteLine(" child this desireddSIze" + ((FrameworkElement)elemento).DesiredSize);


                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).DesiredSize.Width;
                }
            }
        }

        return panelDesiredSize;
    }

I am nesting two linear Layouts: L1 is the parent of L2 and L2 is the parent of 3 buttons.

And the funny thing is that if I write the code below anywhere in the class LinearLayout it works, and the Layout mechanism is called again and it works fine.. but If I call UpdateLayout() nothing happens and the mechanism is not activated(measureoverride ans arrangeoverride are not called)

this.Width = finalSize.Width;
this.Height = finalSize.Height;

Is this a bug? or is just me??? I have been two days already, and it seems to work for the rest of the people ... If anybody can help me I will really appreciate it! By the way I am working with Silverlight for Windows Phone 7...


The measuring phase will set the DesiredSize property, not the Width/Height properties. The Width/Height properties are used to give an element an explicit size, versus auto-fitting to it's content (i.e. via the MeasureOverride).

You shouldn't be setting the Width/Height based on the results of the measure or arrange phase, as these properties are used by the measure and arrange phases to determine the best size.

The UpdateLayout isn't meant to force an element to remeasure/rearrange itself. You would need to use InvalidateMeasure or InvalidateArrange for that task.

EDIT:

Also this line of code isn't really correct:

((FrameworkElement)elemento).Measure(new Size(((FrameworkElement)elemento).Width, ((FrameworkElement)elemento).Height));

You should be passing in the availableSize, or whatever size your panel wants to give the element. The Width/Height may not be valid sizes.

The general rule of thumb is your panel should not touch the Width/Height/MinWidth/MinHeight/etc type properties. It should just pass in an available size, based on the available size it is given. Or you can pass new Size(double.PositiveInfinity, double.PositiveInfinity) when arranging to give an element as much space as it needs.

In your arrange phase, you can use the element.DesiredSize to determine the element's arrangement rect.

0

精彩评论

暂无评论...
验证码 换一张
取 消