Search code examples
c#.netwpfstackpanel

Building a reversible StackPanel in WPF


I'd like to build a custom StackPanel with a ReverseOrder property that I can declaratively set to true to have the elements in the StackPanel appear in the opposite order of normal (e.g. bottom to top or right to left). It needs to be reversible on the fly.

I'm thinking of deriving a new class from StackPanel, but I need to know what methods to override.

Final solution:

protected override System.Windows.Size ArrangeOverride( System.Windows.Size arrangeSize ) {
    double x = 0;
    double y = 0;

    IEnumerable<UIElement> children = ReverseOrder ? InternalChildren.Cast<UIElement>().Reverse<UIElement>() : InternalChildren.Cast<UIElement>();
    foreach ( UIElement child in children ) {
        var size = child.DesiredSize;
        child.Arrange( new Rect( new Point( x, y ), size ) );

        if ( Orientation == Orientation.Horizontal )
            x += size.Width;
        else
            y += size.Height;
    }

    if ( Orientation == Orientation.Horizontal )
        return new Size( x, arrangeSize.Height );
    else
        return new Size( arrangeSize.Width, y );
}

Also define and register ReverseOrder and call UpdateLayout if it changes.


Solution

  • You can reimplement FrameworkElement.ArrangeOverride and invoke all the child.Arrange in the reverse order than usual when necessary.

    http://msdn.microsoft.com/en-us/library/system.windows.uielement.arrange.aspx

    Something like this (not tested):

        double x = 0;
        double y = 0;
    
        var children = ReverseOrder ? InternalChildren.Reverse() : InternalChildren;
        foreach (UIElement child in children)
        {
            var size = child.DesiredSize;
            child.Arrange(new Rect(new Point(x, y), size));
    
            if (Orientation == Horizontal)
                x += size.Width;
            else
                y += size.Height;
        }
    

    Make sure you invoke UpdateLayout after changing ReverseOrder property.