PropertyGrid 中的多行字符串

发布于 2024-07-05 17:19:56 字数 51 浏览 6 评论 0 原文

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

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

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

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

发布评论

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

评论(4

网白 2024-07-12 17:19:56

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

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

I found that System.Design.dll has System.ComponentModel.Design.MultilineStringEditor which can be used as follows:

public class Stuff
{
    [Editor(typeof(MultilineStringEditor), typeof(UITypeEditor))]
    public string MultiLineProperty { get; set; }
}
行雁书 2024-07-12 17:19:56

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

我发现的唯一缺点是我需要用特定的属性来装饰特定的字符串属性。 我已经有一段时间没有这样做了。 我从 Chris Sells 的一本名为“C# 中的 Windows 窗体编程”的书中获得了此信息

有一个名为 Smart PropertyGrid.NET,作者:VisualHint。

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.

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#"

There's a commercial propertygrid called Smart PropertyGrid.NET by VisualHint.

别想她 2024-07-12 17:19:56

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

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

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

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();

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

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();
假情假意假温柔 2024-07-12 17:19:56

是的。 我不太记得它是如何调用的,但是请查看 Items 属性编辑器中是否有类似 ComboBox

Edited 的内容:从 @fryguybob 开始,ComboBox.Items 使用 System.Windows.Forms.Design.ListControlStringCollectionEditor

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

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

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