【问题标题】:PropertyGrid + interfacePropertyGrid + 界面
【发布时间】:2010-04-02 02:39:17
【问题描述】:
public interface ITest {
   void Somethink();
}

public class Test1 : ITest {
   public void Somethink()  { /* do stuff */ }
   public int Test1Property { get; set; }
}

public class Test2 : ITest {
   public void Somethink()  { /* do stuff */ }
   public float Test2Property { get; set; }
}

//Main class
public class MainClass
{
   [TypeConverter(ExpandableObjectConverter)]
   public ITest test { get; set; }
}

好的,我有这样的东西。 MainClass 的实例由 PropertyGrid 选择。

如何制作实现 ITest(此处为 Test1 和 Test2)的类的对象的 DropDownList

【问题讨论】:

    标签: c# .net .net-3.5 propertygrid


    【解决方案1】:

    这不是它的工作原理。测试属性 getter 将返回实现 ITest 的具体类的对象。无论最后分配给它什么,要么为空,要么是 Test1 的对象,要么是 Test2 的对象。 PropertyGrid 使用反射来查看对象类型及其成员。它将显示 Test1Property 或 Test2Property。你无法选择。

    不确定您要做什么,如果您想分配不同类型的对象,您可能需要 UITypeEditor。

    【讨论】:

      【解决方案2】:

      好的,我使用了 UITypeEditor (thx nobugz),并为可能的值创建了组合框。我从Type[] BehaviorManager.GetBehaviorsWhichImplement(Type type) 获得的值 - 返回实现给定接口的类型数组。

      当用户选择一个新值时,我会得到一个使用 Activator.CreateInstance 的选定对象 BehaviorManager.GetBehavior(Type) 的新实例。并将其分配给属性。

      当然,它不是一个下拉列表,但它也很不错:-)

      这是我关注的一篇文章 - http://philwinkel.com/blog/?p=4

      我知道,我的语法很悲惨,对不起,我还在努力解决这个问题 ;-)

      【讨论】:

        猜你喜欢
        • 2015-05-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-31
        • 2011-03-08
        • 1970-01-01
        相关资源
        最近更新 更多