【问题标题】:User Control Docking Attribute用户控制对接属性
【发布时间】:2009-06-08 11:17:08
【问题描述】:

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

【问题讨论】:

  • Control 的 Dock 属性有什么问题?

标签: c# winforms user-controls attributes docking


【解决方案1】:

我还建议查看DockingAttribute

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

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

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

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

PS:很抱歉对一个线程进行了死灵处理。我一直在寻找类似的解决方案,这是谷歌上出现的第一件事。这 设计师属性给了我一个想法,所以我开始四处挖掘 找到了 DockingAttribute,这似乎比公认的要干净得多 具有相同要求结果的解决方案。希望这会有所帮助 未来的某个人。

【讨论】:

    【解决方案2】:

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

    控件设计器:

    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

    这会在设计器中靠近控件右上角显示小“动作箭头”,点击箭头会弹出任务菜单。

    【讨论】:

    • +1。您的帖子非常适合像我这样正在研究自定义控件的设计时支持的人。
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-11
      • 2010-09-05
      相关资源
      最近更新 更多