【问题标题】:Set display text of 'Add'-Dropdown-Button in CollectionEditor在 CollectionEditor 中设置“添加”-Dropdown-Button 的显示文本
【发布时间】:2015-05-13 12:52:53
【问题描述】:

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

下拉菜单显示了我在自定义 CollectionEditor 中返回的类型数组。 但我不希望用户看到类名。我想为用户显示一个有意义的文本。 非常感谢您的帮助!

【问题讨论】:

  • 您能否分享您与下拉列表绑定的对象类
  • 我的对象类很简单。没什么特别的。我还尝试了其他一些类,因为我有多个要使用 UITypeEditor (CollectionEditor) 配置的通用列表。 CollectionEditor 总是显示类名。
  • 请分享代码 sn-p,你是如何绑定你的下拉列表的。
  • 我通过重写 Type[] CreateNewItemTypes() 来绑定自定义 CollectionEditor 中的类。 public class ActionCollectionEditor : CollectionEditor { private Type[] types; public ActionCollectionEditor(Type type) : base(type) { types = new Type[] { typeof(ExportAction), typeof(MinStockViolationsAction), typeof(StoragePlaceAssignmentAction) }; } protected override Type[] CreateNewItemTypes() { return types; } }
  • ...DropdownMenu 显示三个项目:ExportAction、MinStockViolationsAction 和 StoragePlaceAssignmentAction。

标签: c# .net uitypeeditor


【解决方案1】:

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

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;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-09
    • 2021-11-17
    • 1970-01-01
    • 1970-01-01
    • 2020-08-31
    • 1970-01-01
    • 2016-08-07
    • 1970-01-01
    相关资源
    最近更新 更多