WPF WrapPanel 动态高度

4
我有一个包含可变数量控件的WrapPanel。
我希望方向是垂直的(因为内部对象宽度固定,高度可变)。
但是问题在于当滚动条存在时,高度就是无限的,所以项目永远不会换到第二列。由于经常会有比一个屏幕可以容纳的更多的对象,所以滚动条是必要的。我可以通过设置固定高度来解决这个问题,但这不是可接受的解决方案,因为合理的固定高度对于每个选择都不同。
基本上,我想要一个WrapPanel,其高度根据面板的宽度和其中包含的项目数动态变化。
举个例子:
如果面板足够宽,可以显示3列,那么它将: | 1 5 9 | | 2 6 - | | 3 7 - | 高度=4 | 4 8 - |
但是,如果用户更改窗口大小以至于只能容纳2列,则高度将增加: | 1 6 | | 2 7 | | 3 8 | 高度=5 | 4 9 | | 5 - |
此外,我不确定这有多可行,但我理想情况下希望水平排序项目,但保持垂直方向,因此它们将被排序: | 1 2 3 | | 4 5 6 | | 7 8 9 |
有人可以告诉我如何开始进行这项工作吗?我假设使用WrapPanel的自定义实现是可能的,但我有点困惑如何入手。
谢谢。

我不完全确定你想要什么,但看看这个链接是否有帮助:https://dev59.com/QmHVa4cB1Zd3GeqPpbMZ#9770590 - Phil
也许这个可以帮到你,WrapGridPanel: https://dev59.com/uVPTa4cB1Zd3GeqPgR1M - Fredrik Hedblad
1个回答

3

使用以下代码成功实现了我所需的功能:

public class InvertedWrapPanel : WrapPanel
{
    private int itemsPerRow = 0;

    protected override Size MeasureOverride(Size availableSize)
    {
        if (Orientation == Orientation.Horizontal)
        {
            return base.MeasureOverride(availableSize);
        }
        else //Orientation is vertical
        {
            double w = availableSize.Width;

            double maxChildWidth = 0;

            foreach (UIElement child in Children)
            {
                //Get the current childs desired size parameters
                child.Measure(availableSize);

                //Store off the maximum child width
                if (child.DesiredSize.Width > maxChildWidth)
                    maxChildWidth = child.DesiredSize.Width;
            }

            //See how many items we can fit in a row
            itemsPerRow = Convert.ToInt32(Math.Floor(w / maxChildWidth));

            return base.MeasureOverride(availableSize);
        }
    }

    protected override Size ArrangeOverride(Size finalSize)
    {
        if (Orientation == Orientation.Horizontal)
        {
            return base.ArrangeOverride(finalSize);
        }
        else //Orientation is vertical
        {
            double currentX = 0;
            double currentY = 0;

            int col = 0;

            double lastX = 0;
            double lastWidth = 0;

            //Arrays to store differing column heights
            double[] lastY = new double[itemsPerRow];
            double[] lastHeight = new double[itemsPerRow];

            double[] colHeights = new double[itemsPerRow];

            foreach (UIElement child in Children)
            {
                //If we've reached the end of a row
                if (col >= itemsPerRow)
                {
                    col = 0;
                    currentX = 0; //reset the x-coordinate for first column
                }
                else
                    currentX = lastX + lastWidth; //Increase the x-coordinate

                //Increase the y-coordinates for the current column
                currentY = lastY[col] + lastHeight[col];

                //Draw the element
                child.Arrange(new Rect(currentX, currentY, child.DesiredSize.Width, child.DesiredSize.Height));

                //Store off the current child's parameters
                lastX = currentX;
                lastWidth = child.DesiredSize.Width;

                lastY[col] = currentY;
                lastHeight[col] = child.DesiredSize.Height;

                colHeights[col] += child.DesiredSize.Height;

                col++;
            }

            //Set the height of the panel to the max column height.
            //Otherwise scroll bar will set height to infinity.
            double maxHeight = 0;

            foreach (double d in colHeights)
            {
                if (d > maxHeight)
                    maxHeight = d;
            }

            base.Height = maxHeight;

            return finalSize;
        }
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接