【问题标题】:How to add properties to Control designer?如何向控件设计器添加属性?
【发布时间】:2018-02-04 16:07:01
【问题描述】:

我正在与设计师一起制作我的自定义控件(继承自 ControlDesigner)。

我知道如何使用DesignerVerbCollection 添加动词。但是我不知道如何添加与控件属性相关的动词和下拉菜单。

这是一张解释我的意思的图片。

我的 Visual Studio 版本是西班牙语,所以我可以翻译一些例子:

  • Editar elementos... > 编辑项目...
  • 编辑列...> 编辑列...
  • 编辑组...> 编辑组
  • Vista > 查看
  • ImageList pequeña > 小图像列表
  • ImageList grande > 大型 ImageList

我知道Edit items...是动词,但不知道怎么打开集合。

View 和 ImageList 属性是组合框。我不知道如何添加组合框。

编辑:我在问这个问题之前搜索了谷歌,但没有找到与我的问题相关的任何内容。

编辑 2: 我不想将 ListView 属性添加到我的控件中。我只需要将我的自定义属性添加到我的自定义控件的设计器中。

编辑 3: 我检查了 https://referencesource.microsoft.com/#System.Design/System/Windows/Forms/Design/ListViewDesigner.cs,c996e9fb36c3ed37,但 .NET 源代码显示无正文代码。

【问题讨论】:

  • 将图像列表从工具添加到您的控件

标签: c# winforms windows-forms-designer


【解决方案1】:

DesignerActionList 就是您要找的东西。

public class MyControlDesigner : ControlDesigner 
{   
    private DesignerActionListCollection _ActionLists;
    private MyControl myControl;
    
    public MyControlDesigner() : base() 
    {
        _ActionLists = new DesignerActionListCollection();
        _ActionLists.AddRange(base.ActionLists);
        _ActionLists.Add(new MyActionList(this));
    }
    
    public override DesignerActionListCollection ActionLists => _ActionLists;
    public override void Initialize(IComponent component) 
    {
        base.Initialize(component);

        if (Control is MyControl)
            myControl = (MyControl) Control;
    }
    
    public class MyActionList : DesignerActionList
    {
        private MyControlDesigner Designer;
        
        public MyActionList(MyControlDesigner Designer) : base(Designer.Component) => this.Designer = Designer;
        
        public string MyProperty 
        {
            get => Designer.myControl.MyProperty;
            set => Designer.myControl.MyProperty = value;
        }
        
        public override DesignerActionItemCollection GetSortedActionItems() {
            DesignerActionItemCollection items = new DesignerActionItemCollection();
            // Replace Behavior with your property's category (if applicable)
            items.Add(new DesignerActionPropertyItem("MyProperty", "My property", "Behavior", "This is my property in the designer"));
            return items;
        }
    }
}

DesignerActionPropertyItem 用于属性

DesignerActionMethodItem 用于操作

欲了解更多信息:https://msdn.microsoft.com/en-us/library/sey0f414.aspx

【讨论】:

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