带有标题和内容的 UserControl - 允许在内容面板中删除控件并在设计时防止在标题中删除控件

Tha*_*ven 6 .net c# user-controls windows-forms-designer winforms

我写了用户控制(是的!)。但我希望它表现得像一个容器。可是等等!我知道

[Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", 
    typeof(IDesigner))]
Run Code Online (Sandbox Code Playgroud)

诡计。

问题是 - 我不希望我的所有控件都像容器一样,而只是其中的一部分。一个-事实上的-面板;)

为了提供更广泛的上下文:我编写了一个具有网格、一些常见按钮、标签和功能的控件。但它也有一部分用户应该放弃他的自定义按钮/控件。只在这个特定的部分控制,没有其他地方。

有人有任何想法吗?

Rez*_*aei 7

您应该执行以下操作:

  • 对于您的用户控件,您需要创建一个新的设计器,通过调用EnableDesignMode方法在设计时启用内部面板。
  • 对于内部面板,您需要创建一个设计器,它禁止移动、调整大小并从设计器中删除一些属性。
  • 你应该注册设计师。

例子

您可以在此处阅读有关此主题的博客文章并克隆或下载一个工作示例:

在此处输入图片说明

代码

这是解决方案不同元素的代码。

您的用户控制

[Designer(typeof(MyUserControlDesigner))]
public partial class MyUserControl : UserControl
{
    public MyUserControl()
    {
        InitializeComponent();
        TypeDescriptor.AddAttributes(this.panel1,
            new DesignerAttribute(typeof(MyPanelDesigner)));
    }
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public Panel ContentsPanel
    {
        get { return panel1; }
    }
}
Run Code Online (Sandbox Code Playgroud)

内板设计师

public class MyPanelDesigner : ParentControlDesigner
{
    public override SelectionRules SelectionRules
    {
        get
        {
            SelectionRules selectionRules = base.SelectionRules;
            selectionRules &= ~SelectionRules.AllSizeable;
            return selectionRules;
        }
    }
    protected override void PostFilterAttributes(IDictionary attributes)
    {
        base.PostFilterAttributes(attributes);
        attributes[typeof(DockingAttribute)] = 
            new DockingAttribute(DockingBehavior.Never);
    }
    protected override void PostFilterProperties(IDictionary properties)
    {
        base.PostFilterProperties(properties);
        var propertiesToRemove = new string[] {
            "Dock", "Anchor", "Size", "Location", "Width", "Height",
            "MinimumSize", "MaximumSize", "AutoSize", "AutoSizeMode",
            "Visible", "Enabled",
        };
        foreach (var item in propertiesToRemove)
        {
            if (properties.Contains(item))
                properties[item] = TypeDescriptor.CreateProperty(this.Component.GetType(),
                    (PropertyDescriptor)properties[item],
                    new BrowsableAttribute(false));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

用户控件的设计器

public class MyUserControlDesigner : ParentControlDesigner
{
    public override void Initialize(IComponent component)
    {
        base.Initialize(component);
        var contentsPanel = ((MyUserControl)this.Control).ContentsPanel;
        this.EnableDesignMode(contentsPanel, "ContentsPanel");
    }
    public override bool CanParent(Control control)
    {
        return false;
    }
    protected override void OnDragOver(DragEventArgs de)
    {
        de.Effect = DragDropEffects.None;
    }
    protected override IComponent[] CreateToolCore(ToolboxItem tool, int x,
        int y, int width, int height, bool hasLocation, bool hasSize)
    {
        return null;
    }
}
Run Code Online (Sandbox Code Playgroud)