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

标签 c# .net winforms visual-studio-2008 windows-forms-designer

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

一些控件允许以另一种方式改变它们的属性,
除了属性窗口。

看起来像这样:

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

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

最佳答案

那个菜单叫做Smart Tags or Designer Actions并且您可以将智能标记添加到您的控件中。为此,您需要为控件创建自定义 Designer,并在设计器中覆盖其 ActionLists 属性。

示例

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

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

我们的预期结果是:

enter image description here

我的控件

这里我们用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 包含我们需要的 Action 列表项:

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 .

下载范例

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

关于c# - 如何创建这样的任务面板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50801168/

相关文章:

c# - 我怎样才能在 WinForms 应用程序中捕获所有 'unhandled' 异常?

C# 应用程序不关闭

c# - 如何将 JToken 转换为嵌套的 float 列表?

c# - 在 45000 内无法启动套接字

c# - 禁用designer.cs中的自动更改

c# - 为什么没有正确引发数据绑定(bind)完成事件?

c# - System.Drawing.SystemColors 的默认值

c# - DataGridViewComboBoxColumn - 访问 ComboBox 以附加单击事件

c# - 生成具有不同 'Content' 的单选按钮

.net - 免费的 NHibernate 辅助工具?