c# – 用户控制停靠属性

c# – 用户控制停靠属性,第1张

概述我试图让我自己的用户控制并几乎完成它,只是试图添加一些抛光.我希望设计器中的选项“Dock in parent container”.有谁知道如何做到这一点我找不到一个例子.我认为它与Docking Attribute有关. 为了达到这个目的,你需要实现几个类;首先你需要一个自定义 ControlDesigner,然后你需要一个自定义 DesignerActionList.两者都相当简单. Con 我试图让我自己的用户控制并几乎完成它,只是试图添加一些抛光.我希望设计器中的选项“Dock in parent container”.有谁知道如何做到这一点我找不到一个例子.我认为它与Docking Attribute有关.解决方法 为了达到这个目的,你需要实现几个类;首先你需要一个自定义 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.

这将在设计器中显示靠近控件右上角的小“动作箭头”,然后单击箭头将d出任务菜单.

总结

以上是内存溢出为你收集整理的c# – 用户控制停靠属性全部内容,希望文章能够帮你解决c# – 用户控制停靠属性所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1225527.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存