views:

255

answers:

1

Hi all,

I'm trying to create a custom layout container, with the same characteristics of StackPanel, with the exception that it lays out the items starting at the right edge of the screen. Needless to say it does not work correctly.

I have identified a flaw inside ArrangeOverride() where the line

Point elementOrigin = new Point(this.DesiredSize.Width, 0);

simply creates a point @ 0, 0. In other words this.DesiredSize.Width = 0. I understand that the measuring step happens before the arranging step, so I would expect this control will have the DesiredSize property set. How could I start rendering from the right side of the screen otherwise? Is it even possible?

Secondly the finalSize argument that is passed in to the function is much much larger than the area required by the three buttons I have defined in the test xaml. Something to the tune of 1676 by 909 vs a required 250 by 60 or so. Thank you.

Here's my code:

 protected override Size MeasureOverride(Size availableSize)
    {
        Size availableSpace = new Size(double.PositiveInfinity, double.PositiveInfinity);
        Size desiredSize = new Size(0, 0);

        foreach (UIElement child in this.Children)
        {
            child.Measure(availableSpace);
            desiredSize.Width += child.DesiredSize.Width;
            desiredSize.Height = Math.Max(desiredSize.Height, child.DesiredSize.Height);
        }

        return base.MeasureOverride(desiredSize);
    }

    protected override Size ArrangeOverride(Size finalSize)
    {
        Point elementOrigin = new Point(this.DesiredSize.Width, 0);

        foreach (UIElement child in this.Children)
        {
            Rect childBounds = new Rect(elementOrigin, child.DesiredSize);
            elementOrigin.X -= child.DesiredSize.Width;
            child.Arrange(childBounds);
        }   

        return base.ArrangeOverride(finalSize);
    }
+1  A: 

You need to simply return your desiredSize from your MeasureOverride implementation, you don't want to be calling the base version of this method, you are replacing the default implementation with yours.

Similarly with ArrangeOverride you are providing the implementation, you are replacing the default implementation so don't call the base version of this method. Simply return finalSize.

AnthonyWJones