是否有内置的 TypeConverter 或 UITypeEditor 来编辑字符串列表

发布于 2024-08-19 23:29:31 字数 160 浏览 12 评论 0原文

我想知道.Net-3.5是否带有内置的 Liststring[] TypeConverterUITypeEditor 这样我就可以从属性网格编辑此类属性。

I wish to know if .Net-3.5 comes with a built-in List<string> or string[] TypeConverter or UITypeEditor so that I can edit this kind of property from a property grid.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

喵星人汪星人 2024-08-26 23:29:31

对于 List 的 UITypeEditor

对于 string[],您不需要执行任何特殊操作,属性网格将使用包含多行文本框的标准对话框来编辑字符串数组,每一行都是数组中的一个元素。

要在属性网格中编辑 List,您可以使用以下任一选项:

  • StringCollectionEditor,它显示一个包含多行文本框的对话框,用于编辑
  • 元素自定义 CollectionEditor 以编辑集合编辑器对话框中的项目

选项 1 - StringCollectionEditor

private List<string> myList = new List<string>();
[Editor("System.Windows.Forms.Design.StringCollectionEditor, " +
    "System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
    typeof(UITypeEditor))]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public List<string> MyList {
    get {
        return myList;
    }
    set {
        myList = value;
    }
}

在此处输入图像描述

选项 2 - 自定义 CollectionEditor

首先创建自定义编辑器:

//You need to add reference to System.Design
public class MyStringCollectionEditor : CollectionEditor {
    public MyStringCollectionEditor() : base(type: typeof(List<String>)) { }
    protected override object CreateInstance(Type itemType) {
        return string.Empty;
    }
}

然后使用编辑器属性装饰该属性:

private List<string> myList = new List<string>();
[Editor(typeof(MyStringCollectionEditor), typeof(UITypeEditor))]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public List<string> MyList {
    get {
        return myList;
    }
    set {
        myList = value;
    }
}

在此处输入图像描述

UITypeEditor for List<String>

For string[] you don't need to do anything special and the property grid will use a standard dialog containing a multi-line text box to edit string array and each line will be an element in the array.

To edit List<string> in property grid, you can use either of the following options:

  • StringCollectionEditor which shows a dialog containing a multi-line text box to edit elements
  • Create a custom CollectionEditor to edit items in a collection editor dialog

Option 1 - StringCollectionEditor

private List<string> myList = new List<string>();
[Editor("System.Windows.Forms.Design.StringCollectionEditor, " +
    "System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
    typeof(UITypeEditor))]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public List<string> MyList {
    get {
        return myList;
    }
    set {
        myList = value;
    }
}

enter image description here

Option 2 - Custom CollectionEditor

First create the custom editor:

//You need to add reference to System.Design
public class MyStringCollectionEditor : CollectionEditor {
    public MyStringCollectionEditor() : base(type: typeof(List<String>)) { }
    protected override object CreateInstance(Type itemType) {
        return string.Empty;
    }
}

Then decorate the property with the editor attribute:

private List<string> myList = new List<string>();
[Editor(typeof(MyStringCollectionEditor), typeof(UITypeEditor))]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public List<string> MyList {
    get {
        return myList;
    }
    set {
        myList = value;
    }
}

enter image description here

自此以后,行同陌路 2024-08-26 23:29:31

您可以使用 [Editor("System.Windows.Forms.Design.StringArrayEditor, System.Design, [此处的程序集版本和公钥令牌信息]", typeof(System.Drawing.Design.UITypeEditor))]

You can use [Editor("System.Windows.Forms.Design.StringArrayEditor, System.Design, [assembly version and public key token information here]", typeof(System.Drawing.Design.UITypeEditor))]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文