简单地说,使用WPF中的内置面板或容器之一是否有任何方式可以将子项目拉伸到以所需最小尺寸的比例填充宽度。
例如,如果我关闭了最后一个子填充的停靠面板,我可以获得第一张图像的结果。如何水平拉伸这些元素以获得第二张图像。
答案 0 :(得分:0)
事实证明,这比编写自定义面板更容易。对于那些以易于使用的形式追求此类事物的人。
public class StretchPanel : Panel
{
protected override Size MeasureOverride(Size availableSize)
{
Size size = new Size(0, 0);
foreach (UIElement child in Children)
{
child.Measure(availableSize);
size.Width += child.DesiredSize.Width;
size.Height = Math.Max(size.Height, child.DesiredSize.Height);
}
size.Width = double.IsInfinity(availableSize.Width) ?
size.Width : availableSize.Width;
size.Height = double.IsInfinity(availableSize.Height) ?
size.Height : availableSize.Height;
return size;
}
protected override Size ArrangeOverride(Size finalSize)
{
double total = 0;
foreach (UIElement child in Children) total += child.DesiredSize.Width;
double x = 0;
double factor = Math.Max(finalSize.Width / total,1);
foreach (UIElement child in Children)
{
double adjusted_width = child.DesiredSize.Width * factor;
child.Arrange(new Rect(x, 0, adjusted_width, finalSize.Height));
x += adjusted_width;
}
return finalSize;
}
}