gpt4 book ai didi

c# - WPF WrapPanel 动态高度

转载 作者:行者123 更新时间:2023-11-30 12:13:45 27 4
gpt4 key购买 nike

我有一个包含可变数量控件的包装面板。

我希望方向是垂直的(因为其中的对象将具有固定宽度但可变高度)。

但我遇到的问题是,当存在滚动条时,高度是无限的,因此项目永远不会换行到第二列。滚动条是必需的,因为经常会有更多的对象超过一个屏幕上可能容纳的对象。我可以通过设置固定高度来阻止这种情况,但这不是一个可接受的解决方案,因为每个选择的合理固定高度都会有所不同。

基本上我想要一个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 的自定义实现是可能的,但我对如何开始有点困惑。

谢谢,

最佳答案

设法通过以下代码实现我所需要的:

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

关于c# - WPF WrapPanel 动态高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11105404/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com