具有 header 和内容的用户控件 - 在设计时允许在内容面板中放置控件并防止在 header 中放置控件

UserControl with header and content - Allow dropping controls in content panel and Prevent dropping controls in header at design time

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

[Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", 
    typeof(IDesigner))]

技巧。

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

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

有人知道吗?

您应该执行以下操作:

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

例子

您可以阅读有关此主题的博客 post here 并克隆或下载工作示例:

代码

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

您的用户控制权

[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; }
    }
}

内面板设计师

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));
        }
    }
}

用户控件的设计器

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;
    }
}