我通过创建一个继承自 StackPanel 的“StretchStackPanel”在我的应用程序中完成了拉伸部分。该类如下所示:
public class StretchStackPanel : StackPanel
{
public static DependencyProperty StretchDependencyProperty = DependencyProperty.Register("Stretch", typeof(StretchMode), typeof(StretchStackPanel));
protected override Size MeasureOverride(Size availableSize)
{
var baseSize = base.MeasureOverride(availableSize);
if (availableSize.Width != double.PositiveInfinity && (Stretch & StretchMode.Horizontal) == StretchMode.Horizontal )
{
baseSize.Width = availableSize.Width;
}
if (availableSize.Height != double.PositiveInfinity && (Stretch & StretchMode.Vertical) == StretchMode.Vertical)
{
baseSize.Height = availableSize.Height;
}
return baseSize;
}
protected override Size ArrangeOverride(Size finalSize)
{
var baseSize = base.ArrangeOverride(finalSize);
if ((Stretch & StretchMode.Horizontal) == StretchMode.Horizontal )
{
baseSize.Width = finalSize.Width;
}
if ((Stretch & StretchMode.Vertical) == StretchMode.Vertical)
{
baseSize.Height = finalSize.Height;
}
return baseSize;
}
[Category("Layout")]
public StretchMode Stretch
{
get
{
return (StretchMode)GetValue(StretchDependencyProperty);
}
set
{
SetValue(StretchDependencyProperty, value);
}
}
}
你想要的是一个有 2 行的网格。顶行的内容应将其水平对齐设置为居中。底行的内容应该是一个 StretchStackPanel。