用户控件对接属性

发布于 2024-07-23 05:31:21 字数 98 浏览 5 评论 0原文

我正在尝试制作自己的用户控件,并且几乎已经完成,只是想添加一些润色。 我希望设计器中的选项“停靠在父容器中”。 有谁知道如何做到这一点,我找不到例子。 我认为这与对接属性有关。

I am trying to make my own user control and have almost finished it, just trying to add some polish. I would like the option in the designer to "Dock in parent container". Does anyone know how to do this I can't find an example. I think it has something to do with a Docking Attribute.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

帅的被狗咬 2024-07-30 05:31:22

我还建议查看 对接属性

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

这还会在控件的右上角显示“操作箭头”。

此选项早在 .NET 2.0 中就可用,如果您要查找的只是“在父容器中停靠/取消停靠”功能,则该选项要简单得多。 在这种情况下,Designer 类就显得大材小用了。

它还提供了 DockingBehavior.NeverDockingBehavior.AutoDock 选项。 Never 不显示箭头并以其默认的 Dock 行为加载新控件,而 AutoDock 显示箭头但自动将控件停靠为填充

PS:对于死灵术线程感到抱歉。 我一直在寻找类似的解决方案,这是谷歌上出现的第一个解决方案。 这
设计师属性给了我一个想法,所以我开始挖掘并
找到了 DockingAttribute,它看起来比接受的要干净得多
具有相同要求结果的解决方案。 希望这会有所帮助
未来的某个人。

I would also suggest looking at the DockingAttribute.

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

This also displays the 'action arrow' on the upper right corner of the control.

This option is available as far back as .NET 2.0, and it is far simpler if all you're looking for is the 'Dock/Undock in Parent Container' function. The Designer classes are massive overkill in that case.

It also gives the options of DockingBehavior.Never and DockingBehavior.AutoDock. Never does not display the arrow and loads the new control at it's default Dock behavior, while AutoDock displays the arrow but automatically docks the control in as Fill.

PS: Sorry about necromancing a thread. I was looking for a similar solution, and this was the first thing that popped up on Google. The
Designer attributes gave me an idea, so I started digging around and
found DockingAttribute, which seemed far cleaner than the accepted
solution with the same requested results. Hopefully this will help
someone in the future.

南街女流氓 2024-07-30 05:31:22

为了实现这一点,您需要实现几个类; 首先,您需要一个自定义 ControlDesigner然后您将需要一个自定义的 DesignerActionList。 两者都相当简单。

ControlDesigner:

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

DesignerActionList:

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

最后,您需要将设计器附加到您的控件:

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

简要说明

该控件有一个与其关联的 Designer 属性,该属性指出我们的自定义设计师。 该设计器中唯一的自定义是公开的DesignerActionList。 它创建我们的自定义操作列表的实例并将其添加到公开的操作列表集合中。

自定义操作列表包含一个 bool 属性 (DockInParent),并为该属性创建一个操作项。 如果正在编辑的组件的 Dock 属性为 DockStyle.Fill,则该属性本身将返回 true,否则返回 false ,如果 DockInParent 设置为 true,则组件的 Dock 属性设置为 DockStyle.Fill,否则DockStyle.None

这将在设计器中靠近控件右上角的位置显示一个小“操作箭头”,单击该箭头将弹出任务菜单。

I order to achieve this you will need to implement a couple of classes; first you will need a custom ControlDesigner and then you will need a custom DesignerActionList. Both are fairly simple.

The ControlDesigner:

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

The DesignerActionList:

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 designer to your control:

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

Brief explanation

The control has a Designer attribute associated with it, which points out our custom designer. The only customization in that designer is the DesignerActionList that is exposed. It creates an instance of our custom action list and adds it to the action list collection that is exposed.

The custom action list contains a bool property (DockInParent), and creates an action item for that property. The property itself will return true if the Dock property of the component being edited is DockStyle.Fill, otherwise false, and if DockInParent is set to true, the Dock property of the component is set to DockStyle.Fill, otherwise DockStyle.None.

This will display the little "action arrow" close to the upper right corner of the control in the designer, and clicking the arrow will pop up the task menu.

魔法少女 2024-07-30 05:31:22

如果您的控件继承自UserControl(或大多数其他可用控件),则只需将Dock 属性设置为DockStyle.Fill

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文