2010-11-15 126 views

回答

3

您需要爲您的控件創建自己的設計器。通過添加對System.Design的引用開始。示例控件可能如下所示:

using System; 
using System.Windows.Forms; 
using System.ComponentModel; 
using System.ComponentModel.Design; 
using System.Windows.Forms.Design; 

[Designer(typeof(MyControlDesigner))] 
public class MyControl : Control { 
    public bool Prop { get; set; } 
} 

注意[Designer]屬性,它設置自定義控件設計器。爲了讓你開始,從ControlDesigner派生你自己的設計師。重寫ActionLists屬性創建任務列表爲設計師:

internal class MyControlDesigner : ControlDesigner { 
    private DesignerActionListCollection actionLists; 
    public override DesignerActionListCollection ActionLists { 
     get { 
      if (actionLists == null) { 
       actionLists = new DesignerActionListCollection(); 
       actionLists.Add(new MyActionListItem(this)); 
      } 
      return actionLists; 
     } 
    } 
} 

現在,您需要創建自定義ActionListItem,這可能是這樣的:

internal class MyActionListItem : DesignerActionList { 
    public MyActionListItem(ControlDesigner owner) 
     : base(owner.Component) { 
    } 
    public override DesignerActionItemCollection GetSortedActionItems() { 
     var items = new DesignerActionItemCollection(); 
     items.Add(new DesignerActionTextItem("Hello world", "Category1")); 
     items.Add(new DesignerActionPropertyItem("Checked", "Sample checked item")); 
     return items; 
    } 
    public bool Checked { 
     get { return ((MyControl)base.Component).Prop; } 
     set { ((MyControl)base.Component).Prop = value; } 
    } 
} 

在GetSortedActionItems方法構建列表創建您自己的任務項目面板的關鍵。

這是快樂的版本。我應該注意到,在處理這個示例代碼時,我三次將Visual Studio崩潰到桌面。 VS2008是而不是對自定義設計器代碼中未處理的異常具有彈性。經常保存。調試設計時間代碼需要啓動VS的另一個實例,以停止調試器的設計時異常。