簡體   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