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

spa*_*man 5 .net c# visual-studio-2008 windows-forms-designer winforms

在 Visual Studio 2008 中,
如果创建一个窗体并在其上放置一个控件,则
可以通过“属性”窗口编辑控件的属性。


除了“属性”窗口之外,某些控件还可以通过其他方式更改其属性。

它看起来像这样:

似乎所有具有此窗格的控件都具有相同的样式,
这意味着它是由 Visual Studio 提供
的,控件的制造商只是选择要包含在其中的项目,
例如字段和可打开一些链接的可点击链接视窗。

所以我的问题是:
这个窗格控件的名称是什么,
我如何创建一个?

Rez*_*aei 5

该菜单称为智能标签,您可以向控件添加智能标签。为此,您需要Designer为控件创建自定义,并在设计器中覆盖其ActionLists属性。

例子

假设我们已经创建了一个具有一些属性的控件,我们希望在智能标签窗口中显示以下控件的属性:

public Color SomeColorProperty { get; set; }
public string[] Items { get; set; }
Run Code Online (Sandbox Code Playgroud)

我们的预期结果是:

在此处输入图片说明

我的控制

这里我们用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; }
}
Run Code Online (Sandbox Code Playgroud)

我的控制设计器

在这里,我们覆盖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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我的控制操作列表

在这里,我们创建获取/设置控件属性的属性。我们还创建了负责为某些属性显示自定义编辑器或执行某些操作的方法。然后通过覆盖返回操作项列表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"),
        };
    }
}
Run Code Online (Sandbox Code Playgroud)

有关此主题的更多信息,请查看此 MSDN 演练

下载示例

您可以从以下存储库下载工作示例: