4

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

我在配置中存储的其中一件事是 Python 脚本。可以在 StringCollection 编辑器中编辑 Python 脚本,这是我一直在使用的,但“可能”和“可用”之间还有很长的距离。我想要一个真正支持可调整大小和等宽字体的编辑器,保留空白行,并且 - 嘿,让我们为愿望清单发疯 - 进行语法着色。

如果我真的需要,我当然可以写这个,但我不想这样做。

我在谷歌上四处寻找,找不到像我描述的那样的东西,所以我想我会在这里问。这是一个已解决的问题吗?有没有人已经尝试过构建更好的编辑器?

4

2 回答 2

4

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

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

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设置为我们编辑表单的界面。它设置为我们尚未创建的表单,我称之为TextArrayPropertyForm. TextArrayPropertyForm被实例化,传递要编辑的值。为了更好地衡量,我还传递了 2 个字符串,一个用于表单标题,另一个用于顶部的标签,解释用户应该做什么。它以模态方式显示,如果单击“确定”按钮,则使用selectionControl.Value我们将创建的表单中设置的值更新该值。最后,这个值在覆盖结束时返回。

步骤 2) 创建编辑器表单。在我的例子中,我创建了一个带有 2 个按钮(buttonOKbuttonCancel)一个标签(labelInstructions)和一个文本框(textValue)的表单来模仿默认的 StringCollection 编辑器。代码非常简单,但如果你有兴趣,就在这里。

using System;
using System.Windows.Forms;

namespace MyNamespace
{
    /// <summary>
    /// Alternate form for editing string arrays in PropertyGrid control
    /// </summary>
    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)

希望这可以帮助!

于 2013-04-26T20:35:17.607 回答
2

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

一些帮助您入门的资源:

于 2008-11-19T21:23:46.330 回答