2011-12-16 9 views
1

現在、私はPropertyGridを通じていくつかのクラスを構成しようとしています。クラスがプロパティとしてインターフェイスとして提供する場合、今私は値(グリッド内の右側)を表示するいくつかの問題があります。私は、グリッドが反射を使用して実際のタイプを取り、グリッド内の値を表示するのは通常の方法を使用するためだと考えました。あなたは、コンテナを見ることができるようにPropertyGridでインターフェイスプロパティを持つクラスを表示できません

public class MyContainerClass 
{ 
    // Simple properties that should be shown in the PropertyGrid 
    public MyInterface MyInterface { get; set; } 
    public MyClass MyClass { get; set; } 
    public object AsObject { get; set; } 

    public MyContainerClass() 
    { 
     // Create one instance of MyClass 
     var myClass = new MyClass(); 

     // Put the instance into both properties 
     // (cause MyClass implements MyInterface) 
     MyClass = myClass; 
     MyInterface = myClass; 

     // and show it if it is declared as "object" 
     AsObject = myClass; 
    } 
} 

// Some kind of interface i'd like to show in the PropertyGrid. 
public interface MyInterface 
{ 
    string Name { get; set; } 
} 

// A class that also implements the interface 
// and uses some kind of TypeConverter 
[TypeConverter(typeof(ExpandableObjectConverter))] 
public class MyClass : MyInterface 
{ 
    // Create an instance and put something meaningful into the property. 
    public MyClass() 
    { 
     Name = "MyName"; 
    } 

    public string Name { get; set; } 

    // Override ToString() to get something shown 
    // as value in the PropertyGrid. 
    public override string ToString() 
    { 
     return "Overridden ToString(): " + Name; 
    } 
} 

:デモは、単に以下のクラス階層を取り、PropertyGridの持つ単純なフォームを作成し、ここで

propertyGrid1.SelectedObject = new MyContainerClass(); 

を呼び出すことによって、それにオブジェクトを置くためのクラスです3つのプロパティすべてで同じオブジェクトを使用しますが、グリッド内では、クラスプロパティとオブジェクトプロパティには ToString()というテキストが表示されますが、インターフェイスプロパティには何も表示されません。また、TypeConverterは、正確な型を使用するプロパティでのみ使用されます。

PropertyGrid showing MyContainer http://image-upload.de/image/O2CC5e/9558e4e179.png

界面特性の背後にあるクラスのToStringメソッド()の結果を示すPropertyGridのを聞かせする方法が存在しますか?

答えて

1

は最終的に私は、ほとんどの場合のために、この問題を解決:

問題が発生し、少なくとも可視化のためのToString()を呼び出します。そのConverter財産正常に正しいコンバータまたは少なくとも基底クラスTypeConverterPropertyDescriptorリターンを引き起こします。プロパティグリッドは、ReferenceConverterを取得するインターフェイスとして定義されるプロパティの場合が、発言部

に調べることによってReferenceConverterは、典型的には、土地を選定コンポーネントまたは設計環境のコンテキスト内で使用されています。コンポーネントサイトや使用可能なITypeDescriptorContextがなければ、このコンバータはほとんど役に立ちません。

私たちは本当に少し使いそうです。それができた原因、

public override TypeConverter Converter 
{ 
    get 
    { 
     var converter = base.Converter; 

     // If the property of the class is a interface, the default implementation 
     // of PropertyDescriptor will return a ReferenceConverter, but that doesn't 
     // work as expected (normally the right site will stay empty). 
     // Instead we'll return a TypeConverter, that works on the concrete type 
     // and returns at least the ToString() result of the given type. 
     if (_OriginalPropertyDescriptor.PropertyType.IsInterface) 
     { 
      if (converter.GetType() == typeof(ReferenceConverter)) 
      { 
       converter = _InterfaceConverter; 
      } 
     } 

     return converter; 
    } 
} 

ReferenceConverterの明示的なチェックが必要とされている:幸いにも私はすでに私の記述の変換プロパティをオーバーライドし、次に変更し、私自身のPropertyDescriptorをので、私がしなければならなかったすべてして使用していますユーザがプロパティの属性を介して独自のTypeEditorを定義した可能性があります。これは、ベース実装によって自動的に尊重されます。誰かが明示的に属性を使ってReferenceConverterを好きだと言うと問題が発生するだけですが、そのケースを処理する必要はないと思いますが(PropertyDescriptorの属性をチェックすることによって)

関連する問題