为什么在C# Winforms 中控件停靠到它的父级时,设计者设置的"Size" 属性?

Why is the "Size" property of a control set by the designer when the control is docked to its parent in C# Winforms?

考虑 TabelLayoutPanel。我使用 Visual Studio 设计器将它添加到 Form1,并将 Dock 属性 设置为 Fill。即使我使用 Dock 属性 设置了大小,Size 属性 也设置在 Form1.Designer.cs

this.tableLayoutPanel.Dock = System.Windows.Forms.DockStyle.Fill;
this.tableLayoutPanel.Size = new System.Drawing.Size(1000, 800);

这是为什么?设计师为什么要加这一行? Dock 属性 还不够吗?基于此,如果我动态创建此 TableLayoutPanel,我是否应该同时设置 DockSize

您不需要设置Size。设置Dock就够了。

设计器序列化所有值不同于默认值的属性。由于设置Dock属性改变了Size,设计者也将其连载

如果您将 Dock 设置为 None,则序列化后的大小将在以后使用。在 运行 时设置停靠控件的大小也没有任何效果。

Why is the designer adding this line?

这是控件设计者内部工作的结果。 Visual Studio 设计器采用未设置为默认值的每个 属性。 Size 的默认值最终 boils down to the default value of Control.Size, which isn't specified. Hence, it is always generated by the designer. See as an example TabStop 指定默认值的地方:只有在设计器中将其设置为 false 才会生成代码。

它对您 TableLayoutPanel 的视觉最终结果有影响吗?号