【问题标题】:Custom interaction in the PropertyGridPropertyGrid 中的自定义交互
【发布时间】:2013-07-24 20:29:16
【问题描述】:

我有两个自定义类:

class Something
{
}

和:

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

目前,当我在运行时选择class SomethingElse 时,它会填充我的PropertyGrid,向我显示我的所有属性及其相关交互。 即,它说“aPicture”的地方有一个 textbox 字段和一个 button [...] 我可以点击它,它会打开一个对话视图窗口来选择一个图像.

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

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

【问题讨论】:

  • 我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。
  • 您说“当我在运行时选择了SomethingElse 类时” - 不清楚您的意思是什么。你的意思是说“在运行时基于SomethingElse类创建一个对象”?

标签: c# image properties propertygrid


【解决方案1】:

System.Drawing.Image 类有一个自定义的Editor attribute 设置,它定义了一个派生自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”上搜索一些示例。这是官方的一个:Walkthrough: Implementing a UI Type Editor

【讨论】:

  • 我找到了 [Editor] attrb,但不知道如何处理它,非常感谢!演练对我来说是完美的!
猜你喜欢
  • 1970-01-01
  • 2018-10-30
  • 1970-01-01
  • 2021-07-10
  • 2012-08-07
  • 1970-01-01
  • 2011-12-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多