简体   繁体   English

如何创建这样的任务面板?

[英]How to Create such a Task Panel?

In Visual Studio 2008, 在Visual Studio 2008中,
If you create a Form and put a Control on it, 如果您创建一个表单并对其进行控制,
you can edit the control's properties via the Properties window. 您可以通过“属性”窗口编辑控件的属性。

Some controls enable changing their properties in another way, 一些控件允许以另一种方式更改其属性,
in addition to the Properties window. 除了属性窗口。

It looks like this: 它看起来像这样:

It seems that all controls that has this pane, has it in the same style, 似乎所有具有此窗格的控件都具有相同的样式,
meaning it's something that is provided by Visual Studio, 这意味着它是Visual Studio提供的东西,
and the maker of the control just chooses the items to include inside, 并且控制器的制造者只选择要包括在内的物品,
like Fields, and Clickable Links that open some windows. 像Fields,以及打开一些窗口的Clickable Links。

So my question: 所以我的问题:
What is the name of this pane control, 这个窗格控件的名称是什么,
and how do I create one? 以及如何创建一个?

That menu is called smart tags and you can add smart tag to your control. 该菜单称为智能标记,您可以将智能标记添加到控件中。 To do so, you need to create a custom Designer for your control and in the designer, override its ActionLists property. 为此,您需要为控件创建自定义Designer ,并在设计器中覆盖其ActionLists属性。

Example

Let's say we have created a control having some properties, and we want to show the following properties of out control in smart tags window: 假设我们已经创建了一个具有一些属性的控件,并且我们希望在智能标记窗口中显示out控件的以下属性:

public Color SomeColorProperty { get; set; }
public string[] Items { get; set; }

And the expected result for us is: 我们的预期结果是:

在此输入图像描述

MyControl MyControl

Here we decorate the control with Designer attribute to register the custom designer: 这里我们使用Designer属性修饰控件以注册自定义设计器:

using System.ComponentModel;
using System.ComponentModel.Design;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Windows.Forms.Design;

[Designer(typeof(MyControlDesigner))]
public partial class MyControl : UserControl
{
    public MyControl()
    {
        InitializeComponent();
    }
    void InitializeComponent() { }
    public Color SomeColorProperty { get; set; }
    public string[] Items { get; set; }
}

MyControlDesigner MyControlDesigner

Here we override ActionLists and return a new DesignerActionListCollection containing the action list items which we need: 在这里,我们重写ActionLists并返回一个新的DesignerActionListCollection其中包含我们需要的动作列表项:

public class MyControlDesigner : ControlDesigner
{
    private DesignerActionListCollection actionList;
    public override DesignerActionListCollection ActionLists
    {
        get
        {
            if (actionList == null)
                actionList = new DesignerActionListCollection(new[] {
                    new MyControlActionList(this) });
            return actionList;
        }
    }
}

MyControlActionList MyControlActionList

Here we create properties which get/set out control properties. 在这里,我们创建获取/设置控件属性的属性。 Also we create methods which are responsible to show custom editor for some properties or do some actions. 我们还创建了一些方法,负责显示某些属性的自定义编辑器或执行某些操作。 Then return a list of action items by overriding GetSortedActionItems : 然后通过重写GetSortedActionItems返回一个操作项列表:

public class MyControlActionList : DesignerActionList
{
    ControlDesigner designer;
    MyControl control;
    public MyControlActionList(ControlDesigner designer) : base(designer.Component)
    {
        this.designer = designer;
        control = (MyControl)designer.Control;
    }
    public Color SomeColorProperty
    {
        get { return control.SomeColorProperty;  }
        set {
            TypeDescriptor.GetProperties(
                (object)this.Component)["SomeColorProperty"]
                .SetValue((object)this.Component, (object)value);
        }
    }
    public void EditItems()
    {
        var editorServiceContext = typeof(ControlDesigner).Assembly.GetTypes()
            .Where(x => x.Name == "EditorServiceContext").FirstOrDefault();
        var editValue = editorServiceContext.GetMethod("EditValue",
            System.Reflection.BindingFlags.Static |
            System.Reflection.BindingFlags.Public);
        editValue.Invoke(null, new object[] { designer, this.Component, "Items" });
    }

    public override DesignerActionItemCollection GetSortedActionItems()
    {
        return new DesignerActionItemCollection() {
            new DesignerActionMethodItem(this, "EditItems", "Edit Items",  true),
            new DesignerActionPropertyItem("SomeColorProperty", "Some Color"),
        };
    }
}

For more information about this topic, take a look at this MSDN Walkthrough . 有关此主题的更多信息,请查看此MSDN演练

Download Example 下载示例

You can download a working example from the following repository: 您可以从以下存储库下载工作示例:

This is called 'DesignerActionList' or SmartTag. 这称为“DesignerActionList”或SmartTag。 Smart tags are menu-like user interface (UI) elements that supply commonly used design-time options. 智能标签是类似菜单的用户界面(UI)元素,提供常用的设计时选项。

Step: 步:

You must add a reference to the design-time assembly, System.Design.dll 您必须添加对设计时程序集System.Design.dll的引用

Create DesignerActionList class and get the reference to control in the constructor. 创建DesignerActionList类并在构造函数中获取对控件的引用。

public class MyControlTasks : System.ComponentModel.Design.DesignerActionList 
{
  private MyControl myControl;

  private DesignerActionUIService designerActionUISvc = null;

  public MyControlTasks( IComponent component ) : base(component) 
  {
    this.myControl = component as MyControl;
    this.designerActionUISvc =
        GetService(typeof(DesignerActionUIService))
        as DesignerActionUIService;
  }
}

Add methods and properties that you want to associate to smart-tag items 添加要与智能标记项关联的方法和属性

Create base designer for the control 为控件创建基础设计

public interface IDesigner {
   void Dispose();
   void Initialize(IComponent component);
   IComponent Component {
        get;
   }
}

Return a new instance of the MyControlTasks class that you created earlier. 返回您之前创建的MyControlTask​​s类的新实例。

public override DesignerActionListCollection ActionLists
{
    get
    {   
        var actionLists = new DesignerActionListCollection();
        actionLists.Add(new MyControlTasks(this.Component));
        return actionLists;
    }
}

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

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