如何创建这样的任务小组? [英] How to Create such a Task Panel?

查看:0
本文介绍了如何创建这样的任务小组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

某些控件允许以另一种方式更改其属性
除"属性"窗口之外。

如下所示:

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

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

推荐答案

该菜单称为Smart Tags or Designer Actions,您可以将智能标记添加到您的控件。为此,您需要为控件创建自定义Designer,并在设计器中重写其ActionLists属性。

示例

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

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

我们的预期结果是:

MyControl

这里我们使用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

这里我们覆盖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

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

有关此主题的更多信息,请查看this MSDN Walkthrough

下载示例

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

这篇关于如何创建这样的任务小组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆