在CollectionEditor中设置'Add'-Dropdown-Button的显示文本

时间:2015-05-13 12:52:53

标签: c# .net uitypeeditor

如何在自定义CollectionEditor中设置添加按钮的下拉菜单的显示文字?

enter image description here

下拉菜单显示我在自定义CollectionEditor中返回的Type-Array。 但我不希望用户看到类名。我想为用户显示有意义的文本。 我非常感谢你的帮助!

1 个答案:

答案 0 :(得分:0)

据我所知,有两种方法可以在“添加”按钮中操作文本。

1-创建TypeDelegate:How to customize names in "Add" button dropdown in the PropertyGrid custom collection editor

2-连接到CollectionForm的控件(丑陋,不推荐,但另类)

 public sealed class OutputItemEditor : CollectionEditor // need a reference to System.Design.dll
{
    public OutputItemEditor(Type type)
        : base(type)
    {
    }

    protected override Type[] CreateNewItemTypes()
    {
        return new[] { typeof(StaticOutputItem), typeof(VariableOutputItem),typeof(ExpressionOutputItem) };
    }

    protected override CollectionForm CreateCollectionForm()
    {
        CollectionForm collectionForm = base.CreateCollectionForm();            
        collectionForm.Text = "Output Collection";

        //Modify the Add Item Button Text
        try
        {
            //You can use "collectionForm.Controls.Find("addButton",true)" here also
            foreach (ToolStripItem item in collectionForm.Controls[0].Controls[1].Controls[0].ContextMenuStrip.Items)
            {
                //Since Item Names are the Type Names
                switch (item.Text)
                {
                    case "StaticOutputItem":
                        item.Text = "Static Output Item";
                        break;
                    case "VariableOutputItem":
                        item.Text = "Variable Output Item";
                        break;
                    case "ExpressionOutputItem":
                        item.Text = "Expression Output Item";
                        break;
                    default:
                        break;
                }
            }
        }
        catch (Exception)
        {
        }

        return collectionForm;
    }
}