User Control Dock Attribute

I am trying to make my own user control and almost finished it, just trying to add some kind of varnish. I would like the designer to have the option "Docking station in the parent container". Does anyone know how to do this, I cannot find an example. I think this has something to do with the docking attribute.

+3
source share
3 answers

To do this, you will need to implement several classes; first you need a custom ControlDesigner , and then you need a custom DesignerActionList . Both of them are quite simple.

ControlDesigner element:

public class MyUserControlDesigner : ControlDesigner
{

    private DesignerActionListCollection _actionLists;
    public override System.ComponentModel.Design.DesignerActionListCollection ActionLists
    {
        get
        {
            if (_actionLists == null)
            {
                _actionLists = new DesignerActionListCollection();
                _actionLists.Add(new MyUserControlActionList(this));
            }
            return _actionLists;
        }
    }
}

Constructor List:

public class MyUserControlActionList : DesignerActionList
{
    public MyUserControlActionList(MyUserControlDesigner designer) : base(designer.Component) { }

    public override DesignerActionItemCollection GetSortedActionItems()
    {
        DesignerActionItemCollection items = new DesignerActionItemCollection();
        items.Add(new DesignerActionPropertyItem("DockInParent", "Dock in parent"));
        return items;
    }

    public bool DockInParent
    {
        get
        {
            return ((MyUserControl)base.Component).Dock == DockStyle.Fill;
        }
        set
        {
            TypeDescriptor.GetProperties(base.Component)["Dock"].SetValue(base.Component, value ? DockStyle.Fill : DockStyle.None);
        }
    }    
}

Finally, you will need to attach the constructor to your control:

[Designer("NamespaceName.MyUserControlDesigner, AssemblyContainingTheDesigner")]
public partial class MyUserControl : UserControl
{
    // all the code for your control

Brief explanation

The control has an attribute associated with it Designer, which is indicated by our custom constructor. The only setting in this designer is DesignerActionListwhich is displayed. It creates an instance of our custom action list and adds it to the open action list collection.

bool (DockInParent) . true, Dock DockStyle.Fill, false, DockInParent true, Dock DockStyle.Fill, DockStyle.None.

" " , .

+5

DockingAttribute.

[Docking(DockingBehavior.Ask)]
public class MyControl : UserControl
{
    public MyControl() { }
}

" " .

.NET 2.0, , , , "-/ ". Designer .

DockingBehavior.Never DockingBehavior.AutoDock. Never Dock, AutoDock , Fill.

PS: . , , Google. , DockingAttribute, , . , - .

+14

If your control inherits from UserControl(or most of the other available controls), you just need to set the Dockvalue of the property DockStyle.Fill.

+3
source

Source: https://habr.com/ru/post/1709974/


All Articles