有WPF吗;WrapGrid“;控件可用还是创建控件的简单方法?
基本上,我想要一个包装,但我希望项目捕捉到一个网格,而不是被压到左边,这样我可以得到一个漂亮的统一外观的网格,自动消耗可用空间有WPF吗;WrapGrid“;控件可用还是创建控件的简单方法?,wpf,layout,grid,wrappanel,Wpf,Layout,Grid,Wrappanel,基本上,我想要一个包装,但我希望项目捕捉到一个网格,而不是被压到左边,这样我可以得到一个漂亮的统一外观的网格,自动消耗可用空间 protected override Size ArrangeOverride(Size finalSize) { double rowY = 0; int col = 0; double currentRowHeight = 0; foreach (UIElement child in C
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处理调整大小的零件。
WPF.Contrib.autoglid处理一个漂亮的自动网格
有人得到一个结合了它们的控件吗
我的用例是我有一系列形状有些不规则的控件。我希望它们显示在漂亮的列中,以便在放置控件时,包裹面板应捕捉到下一个“tabstop”尝试设置包裹面板的属性(或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>
当我读到你的问题时,我以为你想要这样的东西:
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”,从您的评论中可以看出,这不是您想要的
我不明白的是:
我希望它不会强制项目具有给定的大小,而是使用它们已经存在的大小,从而自动确定列宽
你能举一个例子来说明你想要的东西是如何以不同的尺寸摆放的吗?一张照片可能很好。您还提到了“tabstop”,但没有给出任何定义。以下是我根据其他一些接近的控件编写的代码。它在布局方面做得不错,尽管它存在一个问题,即grand child控件没有填满所有可用空间
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);
}
UniformWrapPanel与我想要的非常接近,只是我希望它不强制项目为给定的大小,而是使用它们已经存在的大小,从而自动确定列宽