是否有更好的StringCollection编辑器可以在PropertyGrids中使用?

我在应用程序框架的配置编辑器中大量使用PropertySheets。 我非常喜欢它们,因为它很容易与它们一起工作(一旦你学会了)并使编辑变得无懈可击。

我在配置中存储的一件事是Python脚本。 可以在StringCollection编辑器中编辑Python脚本,这是我一直在使用的,但是“可能”和“可用”之间有很长的距离。 我想有一个实际上支持可resize和等宽字体的编辑器,保留空行,并且 – 嘿,让我们对wishlist疯狂 – 做语法着色。

如果我真的需要,我当然可以写这个,但我不愿意。

我在谷歌上搜索过,找不到我所描述的内容,所以我想我会在这里问。 这是一个解决的问题吗? 有没有人已经在建立一个更好的编辑器?

您可以按照以下简单步骤轻松创建自己的字符串集合编辑器。 此示例使用C#。

1)您必须创建一个编辑器控件并从System.Drawing.Design.UITypeEditor派生它。 我打电话给我的StringArrayEditor 。 因此,我的class级开始

 public class StringArrayEditor : System.Drawing.Design.UITypeEditor 

PropertyGrid控件需要知道编辑器是模态的,并且当选择了相关属性时它将显示省略号按钮。 所以你必须覆盖GetEditStyle ,如下所示:

  public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) { return UITypeEditorEditStyle.Modal; } 

最后,编辑器控件必须覆盖EditValue操作,以便在用户单击属性的省略号按钮时知道如何继续。 以下是覆盖的完整代码:

  public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) { var editorService = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService; if (editorService != null) { var selectionControl = new TextArrayPropertyForm((string[])value, "Edit the lines of text", "Label Editor"); editorService.ShowDialog(selectionControl); if (selectionControl.DialogResult == DialogResult.OK) value = selectionControl.Value; } return value ?? new string[] {}; } 

那么发生了什么? 当用户单击省略号时,将调用此覆盖。 editorService被设置为编辑表单的界面。 它被设置为我们尚未创建的forms,我称之为TextArrayPropertyFormTextArrayPropertyForm被实例化,传递要编辑的值。 为了更好的衡量,我也传递了两个字符串,一个用于表单标题,另一个用于顶部的标签,用于解释用户应该做什么。 它以模态显示,如果单击了OK按钮,则使用我们将创建的表单中的selectionControl.Value中设置的值更新值。 最后,在覆盖结束时返回此值。

步骤2)创建编辑器表单。 在我的例子中,我创建了一个带有2个按钮( buttonOKbuttonCancel )的labelInstructionslabelInstructions )和一个TextBox( textValue )来模仿默认的StringCollection编辑器。 代码很简单,但是如果你感兴趣的话,就在这里。

 using System; using System.Windows.Forms; namespace MyNamespace { ///  /// Alternate form for editing string arrays in PropertyGrid control ///  public partial class TextArrayPropertyForm : Form { public TextArrayPropertyForm(string[] value, string instructions = "Enter the strings in the collection (one per line):", string title = "String Collection Editor") { InitializeComponent(); Value = value; textValue.Text = string.Join("\r\n", value); labelInstructions.Text = instructions; Text = title; } public string[] Value; private void buttonCancel_Click(object sender, EventArgs e) { DialogResult = DialogResult.Cancel; } private void buttonOK_Click(object sender, EventArgs e) { Value = textValue.Text.Split(new[] { "\r\n" }, StringSplitOptions.None); DialogResult = DialogResult.OK; } } } 

步骤3)告诉PropertyGrid使用备用编辑器。 此属性与PropertyGrid控件中使用的任何其他属性之间的更改是[Editor]行。

  [Description("The name or text to appear on the layout.")] [DisplayName("Text"), Browsable(true), Category("Design")] [Editor(typeof(StringArrayEditor), typeof(System.Drawing.Design.UITypeEditor))] public string[] Text {get; set;} 

现在,当您在窗体上创建一个PropertyGrid并设置为包含此Text属性的类时,它将在您自定义的窗体中进行编辑。 有无数的机会以您选择的方式更改您的自定义表单。 通过修改,这将适用于编辑您喜欢的任何类型。 重要的是编辑器控件返回的类型与重写的EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)的属性相同EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)

希望这可以帮助!

您需要编写自己的类型编辑器。 您可以将其视为用户控件,因为当您编写自己的类型编辑器时,您将提供在属性网格编辑属性时显示的UI控件。 因此,您可以创建一个可以执行任何操作的类型编辑器,这意味着如果您有第三方编辑器控件,则可以将其作为类型编辑器的一部分包含在内。

一些资源可以帮助您入门:

  • 用户界面类型编辑器概述
  • 演练:实现UI类型编辑器
  • UITypeEditor类
  • 如何:实现UI类型编辑器
  • 使用UITypeEditors进行丰富的设计时编辑(VB.NET)
  • 在DesignTime for VS.Net中轻松创建属性编辑器(UITypeEditor Helper)
  • 使用PropertyGrid