在WPF中构建可逆的StackPanel

问题描述 投票:9回答:3

我想构建一个带有StackPanel属性的自定义ReverseOrder,我可以声明性地将其设置为true,以使StackPanel中的元素以正常的相反顺序出现(例如从下到上或从右到左)。它需要在运行中可逆。

我正在考虑从StackPanel派生一个新类,但我需要知道要覆盖哪些方法。

最终盐:

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 );
}

同时定义并注册ReverseOrder并调用UpdateLayout(如果它发生变化)。

c# .net wpf stackpanel
3个回答
12
投票

您可以重新实现FrameworkElement.ArrangeOverride并在必要时以相反的顺序调用所有child.Arrange。

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

像这样的东西(未经测试):

    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;
    }

确保在更改ReverseOrder属性后调用UpdateLayout。


3
投票

MeasureOverrideArrangeOverride

实际上,您的度量逻辑可能与StackPanel相同,因此您可能只能覆盖ArrangeOverride。请注意,StackPanel有一些处理滚动的逻辑,如果您自己编写,可能需要复制。您可能希望直接从Panel继承而不是尝试支持滚动。

有关编写自定义面板(如Custom Panel ElementsWPF Tutorial - Creating A Custom Panel Control)的面板上的MSDN页面或各种博客条目,请参阅Creating Custom Panels In WPF


0
投票

这是另一种变体。它基于StackPanelreference source,并且能够处理项目对齐。

public class ReversibleStackPanel : StackPanel
{
    public bool ReverseOrder
    {
        get => (bool)GetValue(ReverseOrderProperty);
        set => SetValue(ReverseOrderProperty, value);
    }

    public static readonly DependencyProperty ReverseOrderProperty =
        DependencyProperty.Register(nameof(ReverseOrder), typeof(bool), typeof(ReversibleStackPanel), new PropertyMetadata(false));


    protected override Size ArrangeOverride(Size arrangeSize)
    {
        bool fHorizontal = (Orientation == Orientation.Horizontal);
        var  rcChild = new Rect(arrangeSize);
        double previousChildSize = 0.0;

        var children = ReverseOrder ? InternalChildren.Cast<UIElement>().Reverse() : InternalChildren.Cast<UIElement>();
        foreach (UIElement child in children)
        {
            if (child == null)
                continue;

            if (fHorizontal)
            {
                rcChild.X += previousChildSize;
                previousChildSize = child.DesiredSize.Width;
                rcChild.Width = previousChildSize;
                rcChild.Height = Math.Max(arrangeSize.Height, child.DesiredSize.Height);
            }
            else
            {
                rcChild.Y += previousChildSize;
                previousChildSize = child.DesiredSize.Height;
                rcChild.Height = previousChildSize;
                rcChild.Width = Math.Max(arrangeSize.Width, child.DesiredSize.Width);
            }

            child.Arrange(rcChild);
        }

        return arrangeSize;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.