我正在使用 TableLayoutPanel 将客户区拆分为 3 行(只有 1 列)。顶排和底排设计为固定高度;它们将包含一个页眉和一个页脚,最初每个都包含一个包含静态文本的子标签控件(只是开始)。中间行应动态调整大小以填充剩余区域。这个中间窗格最终将包含一个列表视图。我有一个管理器类,它将被管理的面板(ExplorerTableLayoutPanel)对象作为参数:
public class ExplorerTableLayoutPanelManager
{
public ExplorerTableLayoutPanelManager(ExplorerTableLayoutPanel panel)
{
LayoutPanel = panel;
}
有 3 种方法可以在表格布局中创建 3 行中的每一行:
private void AddHeaderRow()
{
const int headerHeight = 30;
LayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, headerHeight));
Label label = new Label();
label.BackColor = Color.AliceBlue;
label.BorderStyle = BorderStyle.None;
label.ForeColor = Color.LightGray;
label.TextAlign = ContentAlignment.MiddleRight;
label.Text = "Header Banner";
label.Dock = DockStyle.Fill;
float size = label.Font.SizeInPoints;
label.Font = new Font(label.Font.Name, size * 2);
const int column = 0, row = 0;
LayoutPanel.Controls.Add(label, column, row);
}
private void AddBodyRow()
{
LayoutPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
Label label = new Label();
label.BorderStyle = BorderStyle.FixedSingle;
label.ForeColor = Color.LightGray;
label.TextAlign = ContentAlignment.MiddleCenter;
label.Text = "Content Under construction ...";
label.Dock = DockStyle.Fill;
float size = label.Font.SizeInPoints;
label.Font = new Font(label.Font.Name, size * 2);
const int column = 0, row = 1;
LayoutPanel.Controls.Add(label, column, row);
}
private void AddFooterRoow()
{
const int footerHeight = 30;
LayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, footerHeight));
Label label = new Label();
label.BackColor = Color.AliceBlue;
label.BorderStyle = BorderStyle.None;
label.ForeColor = Color.LightGray;
label.TextAlign = ContentAlignment.MiddleRight;
label.Text = "Footer Banner";
label.Dock = DockStyle.Fill;
float size = label.Font.SizeInPoints;
label.Font = new Font(label.Font.Name, size * 2);
const int column = 0, row = 2;
LayoutPanel.Controls.Add(label, column, row);
}
我看到的问题是最后一行占用了我要求的固定行高 30。这部分是正确的。但是,第一行和第二行在它们之间平均分配剩余空间,这不是我想要的。如您所见,我已将第一行的行高显式设置为 30,其方式与最后一行完全相同,但这似乎不起作用。第 2 行(中间)的 RowStyle 大小设置为 SizeType.AutoSize,虽然我的意思是用完剩余空间,所以不要明确设置大小。我可能错了,但我不确定。