我正在为Windows窗体编写具有自动布局的自定义容器。 microsoft文档建议使用以下代码来遵守控件的autosize属性:
if (control.AutoSize)
{
control.Size = control.GetPreferredSize(parentDisplayRectangle.Size);
}
问题在于文本框似乎已将AutoSize硬编码为true,并在其上调用所提到的代码,如下所示:
我应该如何评估自定义容器控件中文本框的首选大小?
更新:访问Referencesource之后。
DefaultLayout类中有一个有用的注释:
// Loop through the AutoSized controls and expand them if they are smaller than
// their preferred size. If expanding the controls causes overlap,bump the overlapped
// control if it is AutoRelocatable.
AutoSize的想法似乎是允许控件大于设计的大小,而不是实际上精确地从GetPreferredSize返回的大小。
这有待确认。