繁体   English   中英

如何向控件设计器添加属性?

[英]How to add properties to Control designer?

我正在使用设计器(从ControlDesigner继承)制作我的自定义控件。

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

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

设计师

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

  • 编辑器元素... > 编辑项目...
  • 编辑列作为... > 编辑列...
  • 编辑组... > 编辑组
  • 远景 > 查看
  • ImageList pequeña > Small ImageList
  • 大图像列表 > 大图像列表

我知道Edit items...是一个动词,但我不知道如何打开集合。

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

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM