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

时间:2018-06-08 10:54:41

标签: c# .net winforms user-controls windows-forms-designer

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

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

圈套。

问题是 - 我不希望我的所有控件都像容器一样,但只有一部分。一个 - 事实上 - 小组;)

为了给出更广泛的背景:我编写了一个具有Grid的控件,一些常用的按钮,标签和功能。但它也有一个部分,用户应该放弃他的自定义按钮/控制任何东西。只有在控制的这个特定部分,没有其他地方。

任何人都有任何想法?

1 个答案:

答案 0 :(得分:2)

您应该执行以下操作:

  • 对于您的用户控件,您需要创建一个新的设计器,通过调用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; }
    }
}

内部面板的设计器

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

示例

您可以阅读有关此主题here的博文,并克隆或下载一个有效的示例:

enter image description here