首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用户控件停靠属性

用户控件停靠属性
EN

Stack Overflow用户
提问于 2009-06-08 11:17:08
回答 3查看 6.4K关注 0票数 5

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2009-06-08 12:08:49

为了实现这一点,你需要实现几个类;首先你需要一个定制的ControlDesigner,然后你需要一个定制的DesignerActionList。这两种方法都相当简单。

ControlDesigner:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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);
        }
    }    
}

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

代码语言:javascript
复制
[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

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

票数 6
EN

Stack Overflow用户

发布于 2012-04-07 04:58:30

我还建议你看看DockingAttribute

代码语言:javascript
复制
[Docking(DockingBehavior.Ask)]
public class MyControl : UserControl
{
    public MyControl() { }
}

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

这个选项早在.NET 2.0中就有了,如果你想要的只是“在父容器中停靠/取消停靠”功能,那么它会简单得多。在这种情况下,设计器类被大量夸大了。

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

PS:很抱歉对一个线程进行了巫术。我正在寻找一个类似的解决方案,这是谷歌上出现的第一件事。设计器属性给了我一个想法,所以我开始深入研究,找到了DockingAttribute,它似乎比接受的解决方案干净得多,并具有相同的要求结果。希望这能在未来帮助某些人。

票数 16
EN

Stack Overflow用户

发布于 2009-06-08 11:47:22

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

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/964421

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档