2010-11-15 47 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的另一个实例,以停止调试器的设计时异常。

相关问题