本质上我想要一个 wrapPanel,但我希望项目捕捉到网格而不是被压到左边,所以我可以获得一个漂亮的均匀网格,它会自动消耗可用空间。
WrapPanel 处理调整大小部分。WPF.Contrib.AutoGrid 处理一个很好的自动网格。
任何人有一个结合它们的控件?
我的用例是我有一系列形状不规则的控件。我希望它们出现在漂亮的列中,以便在放置控件时自动换行面板应该捕捉到下一个“制表位”
当我读到你的问题时,我假设你想要这样的东西:
public class UniformWrapPanel : WrapPanel
{
protected override Size MeasureOverride(Size constraint)
{
if(Orientation == Orientation.Horizontal)
ItemWidth = Children.Select(element =>
{
element.Measure(constraint);
return element.DesiredWidth;
}).Max();
else
... same for vertical ...
return base.MeasureOverride(constraint);
}
}
但我看到其他人已经实现了“UniformWrapPanel”,从你的评论中你表明这不是你想要的。
我不明白的评论是:
我希望它不强制项目为给定大小,而是使用它们已经存在的大小,因此自动确定列宽
您能否提供一个示例来说明您希望如何以不同的尺寸布置物品?一张图可能不错。您还提到了“制表符”,但没有给出任何定义。
这是我根据其他一些关闭的控件编写的一些代码。它在布局方面做得不错,尽管它存在孙控件无法填满所有可用空间的问题。
protected override Size ArrangeOverride(Size finalSize)
{
double rowY = 0;
int col = 0;
double currentRowHeight = 0;
foreach (UIElement child in Children)
{
var initialSize = child.DesiredSize;
int colspan = (int) Math.Ceiling(initialSize.Width/ ColumnSize);
Console.WriteLine(colspan);
double width = colspan * ColumnSize;
if (col > 0 && (col * ColumnSize) + width > constrainedSize.Width)
{
rowY += currentRowHeight;
col = 0;
currentRowHeight = 0;
}
var childRect = new Rect(col * ColumnSize, rowY, width, initialSize.Height);
child.Arrange(childRect);
currentRowHeight = Math.Max(currentRowHeight, initialSize.Height);
col+=colspan;
}
return finalSize;
}
Size constrainedSize;
protected override Size MeasureOverride(Size constraint)
{
constrainedSize = constraint;
return base.MeasureOverride(constraint);
}
尝试设置WrapPanel 的ItemWidth(或ItemHeight
)属性:
<WrapPanel ItemWidth="48">
<TextBlock Text="H" Background="Red"/>
<TextBlock Text="e" Background="Orange"/>
<TextBlock Text="l" Background="Yellow"/>
<TextBlock Text="l" Background="Green"/>
<TextBlock Text="o" Background="Blue"/>
<TextBlock Text="!" Background="Violet"/>
</WrapPanel>