PropertyGrid 中是否有多行字符串的内置编辑器。

有帮助吗?

解决方案

我发现 System.Design.dll System.ComponentModel.Design.MultilineStringEditor ,可以按如下方式使用:

public class Stuff
{
    [Editor(typeof(MultilineStringEditor), typeof(UITypeEditor))]
    public string MultiLineProperty { get; set; }
}

其他提示

不,您需要创建所谓的模态UI类型编辑器。您需要创建一个继承自UITypeEditor的类。这基本上是当您单击正在编辑的属性右侧的省略号按钮时显示的表单。

我发现的唯一缺点是我需要使用特定属性修饰特定的字符串属性。我必须这样做一段时间。我从Chris Sells的一本书中获得了这个信息,称为“用C#进行Windows窗体编程”

VisualHint有一个名为 Smart PropertyGrid.NET 的商业属性网格。

是。我不太清楚它是如何被调用的,但是看看Items属性编辑器中的ComboBox

编辑:从@fryguybob开始,ComboBox.Items使用System.Windows.Forms.Design.ListControlStringCollectionEditor

我们需要编写自定义编辑器以获取属性网格中的多行支持。

以下是从 UITypeEditor

public class MultiLineTextEditor : UITypeEditor
{
    private IWindowsFormsEditorService _editorService;

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

    public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
    {
        _editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));

        TextBox textEditorBox = new TextBox();
        textEditorBox.Multiline = true;
        textEditorBox.ScrollBars = ScrollBars.Vertical;
        textEditorBox.Width = 250;
        textEditorBox.Height = 150;
        textEditorBox.BorderStyle = BorderStyle.None;
        textEditorBox.AcceptsReturn = true;
        textEditorBox.Text = value as string;

        _editorService.DropDownControl(textEditorBox);

        return textEditorBox.Text;
    }
}

编写自定义属性网格并将此Editor属性应用于属性

class CustomPropertyGrid
{
    private string multiLineStr = string.Empty;

    [Editor(typeof(MultiLineTextEditor), typeof(UITypeEditor))]
    public string MultiLineStr
    {
        get { return multiLineStr; }
        set { multiLineStr = value; }
    }
}

在主窗体中指定此对象

 propertyGrid1.SelectedObject = new CustomPropertyGrid();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top