PropertyGrid中的自定义交互

本文关键字:交互 自定义 PropertyGrid | 更新日期: 2023-09-27 18:24:41

我有两个自定义类:

class Something
{
}

和:

class SomethingElse
{
public Something newSomething {get;set;}
public String aName {get;set;}
public Image aPicture {get;set;}
//etc...
}

目前,当我在运行时选择class SomethingElse时,它会填充我的PropertyGrid,向我显示我的所有属性及其相对交互。也就是说,在它说"aPicture"的地方,有一个文本框字段和一个按钮[…],我可以点击它,它会打开一个对话框视图窗口来选择图像。

如何将此功能添加到我的自定义类中?所以,当属性网格视图显示newSomething时,它会显示一个文本框和一个按钮?

我完全迷失了方向,甚至不知道我需要看什么/在哪里。我看了Image class,但看不到任何明显的东西。

PropertyGrid中的自定义交互

System.Drawing.Image类有一个自定义的Editor属性设置,它定义了一个从基本UITypeEditor类派生的类,如果你用.NET Reflector等工具查看它,你会看到:

[... Editor("System.Drawing.Design.ImageEditor, System.Drawing.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))...]
public abstract class Image : ...
{
   ...
}

所以你可以对你的课做同样的事情,比如:

[Editor(typeof(MySomethingEditor), typeof(UITypeEditor))]
public class Something
{
   ...
}
public class MySomethingEditor: UITypeEditor
{
   ...
}

您可以在"UITypeEditor"上搜索以获取一些示例。这里有一个官方版本:演练:实现UI类型编辑器