C# PropertyGrid 中的多行字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/130032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 14:53:46  来源:igfitidea点击:

Multi-line string in a PropertyGrid

提问by fryguybob

Is there a built-in editor for a multi-line string in a PropertyGrid.

.a 文件中是否有用于多行字符串的内置编辑器PropertyGrid

采纳答案by fryguybob

I found that System.Design.dllhas System.ComponentModel.Design.MultilineStringEditorwhich can be used as follows:

我发现,System.Design.dllSystem.ComponentModel.Design.MultilineStringEditor哪些可以如下使用:

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

回答by Hector Sosa Jr

No, you will need to create what's called a modal UI type editor. You'll need to create a class that inherits from UITypeEditor. This is basically a form that gets shown when you click on the ellipsis button on the right side of the property you are editing.

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

The only drawback I found, was that I needed to decorate the specific string property with a specific attribute. It's been a while since I had to do that. I got this information from a book by Chris Sells called "Windows Forms Programming in C#"

我发现的唯一缺点是我需要用特定的属性装饰特定的字符串属性。自从我不得不这样做以来已经有一段时间了。我从 Chris Sells 的一本名为“Windows Forms Programming in C#”的书中得到了这些信息

There's a commercial propertygrid called Smart PropertyGrid.NETby VisualHint.

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

回答by Ilya Ryzhenkov

Yes. I don't quite remember how it is called, but look at the Items property editor for something like ComboBox

是的。我不太记得它是如何调用的,但是查看 Items 属性编辑器中的 ComboBox 之类的东西

Edited: As of @fryguybob, ComboBox.Items uses the System.Windows.Forms.Design.ListControlStringCollectionEditor

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

回答by Sunil Kumar

We need to write our custom editor to get the multiline support in property grid.

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

Here is the customer text editor class implemented from UITypeEditor

这是从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;
    }
}

Write your custom property grid and apply this Editor attribute to the property

编写您的自定义属性网格并将此编辑器属性应用于该属性

class CustomPropertyGrid
{
    private string multiLineStr = string.Empty;

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

In main form assign this object

在主窗体中分配这个对象

 propertyGrid1.SelectedObject = new CustomPropertyGrid();