Add child to custom Control in Designer mode

无人久伴 提交于 2019-12-12 02:44:45

问题


I have already seen this answer for adding designer support to a custom Control. It works up to and including adding child Controls. Except that after closing the designer and opening it again - those children are gone.

Is there some way to get support for adding child Controls in the designer? Perhaps by some event that we can handle somehow "manually" (i.e. by my code)?

This is not a UserControl. It's a class inheriting from Panel.


回答1:


You need to shadow or override the Controls property and apply [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)].

This will tell the form's designer to save the contents of that property to its .Designer.cs.




回答2:


For a class derived from Panel, the designer is enabled by default and you don't need to do anything, because the designer of panel is PanelDesigner which is derived from ScrollableControlDeigner which is derived from ParentControlDesigner:

ParentControlDesigner provides a base class for designers of controls that can contain child controls. In addition to the methods and functionality inherited from the ControlDesigner and ComponentDesigner classes, ParentControlDesigner enables child controls to be added to, removed from, selected within, and arranged within the control whose behavior it extends at design time.

You can enable the designer by setting ParentControldesigner as designer of your control. For example:

using System.ComponentModel;
using System.Windows.Forms;
using System.Windows.Forms.Design;
[Designer(typeof(ParentControlDesigner))]
public class MyControl : Control
{
}


来源:https://stackoverflow.com/questions/44144698/add-child-to-custom-control-in-designer-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!