简体   繁体   English

调整面板控件中停靠的用户控件的大小不会调整面板控件的大小-更好的解决方案?

[英]Resizing user control that is docked in Panel control does not resize Panel control - better solution?

I have a user control that has two size options (there is a button in the user control, to toggle between the two options) - something like section - collapsed/expanded. 我有一个具有两个大小选项的用户控件(用户控件中有一个按钮,可在两个选项之间切换)-类似于section-折叠/展开。

This user control is placed on a Panel control and Docked to fill it. 该用户控件放置在面板控件上,并停靠以填充它。

I need this Panel to be re-sized according to the user control's size. 我需要根据用户控件的大小调整此面板的大小。 I expected it to happen automatically, since user control is Docked to fill the Panel, but Panel's SizeChanged event is not triggered. 我希望它会自动发生,因为用户控件已停靠以填充面板,但是不会触发Panel的SizeChanged事件。

So my solution is to have an event SizeChanged for user control and eventhandler in container form, that adjusts the Panel's size correspondingly. 因此,我的解决方案是以容器形式为用户控件和事件处理程序提供一个事件SizeChanged ,该事件相应地调整面板的大小。

But shouldn't this work automatically (as I mentioned above)? 但是,这不应该自动工作吗(如上所述)? If not, then why changes to user control's size is not triggering container control's SizeChanged event? 如果不是,那么为什么更改用户控件的大小不会触发容器控件的SizeChanged事件?

Well, you don't want Dock since that forces the control's size to match the panel size. 好吧,您不希望使用Dock,因为这会强制控件的大小与面板大小匹配。 To do it the other way around, you need to set the panel's AutoSize property to True, AutoSizeMode to GrowAndShrink. 为此,您需要将面板的AutoSize属性设置为True,将AutoSizeMode设置为GrowAndShrink。 And use Padding or the control's Margin to tweak and make the panel a bit bigger. 并使用Padding或控件的Margin进行调整,使面板更大。

Do keep in mind that the changing panel size tends to give you more layout problems with the rest of the window, stuff may have to move out of the way to provide space. 请记住,面板尺寸的变化会给您其余窗口带来更多的布局问题,某些东西可能不得不移开以提供空间。 FlowLayoutPanel tends to be the next choice. FlowLayoutPanel往往是下一个选择。 Or the panel's SizeChanged event when automatic layout can't tackle it anymore. 或者自动布局无法解决时面板的SizeChanged事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM