2

このオブジェクトをバインドすると

public class MyObject
{ 
  public AgeWrapper Age
{
get;
set;
}
}

public class AgeWrapper
{
public int Age
{
get;
set;
}
}

プロパティ グリッドの値セクションに表示されるのは、AgeWrapper のクラス名ですが、AgeWrapper.Age の値です。

プロパティ グリッドで、複合オブジェクトのクラス名の代わりに、複合オブジェクト (この場合は AgeWrapper.Age) の値を表示できるようにする方法はありますか?

4

1 に答える 1

5

型コンバーターを作成し、属性を使用してそれをAgeWrapperクラスに適用する必要があります。次に、プロパティグリッドは、文字列を表示するためにその型コンバーターを使用します。このような型コンバーターを作成します...

public class AgeWrapperConverter : ExpandableObjectConverter
{
  public override bool CanConvertTo(ITypeDescriptorContext context, 
                                    Type destinationType)
  {
    // Can always convert to a string representation
    if (destinationType == typeof(string))
      return true;

    // Let base class do standard processing
    return base.CanConvertTo(context, destinationType);
  }

  public override object ConvertTo(ITypeDescriptorContext context, 
                                   System.Globalization.CultureInfo culture, 
                                   object value, 
                                   Type destinationType)
  {
    // Can always convert to a string representation
    if (destinationType == typeof(string))
    {
      AgeWrapper wrapper = (AgeWrapper)value;
      return "Age is " + wrapper.Age.ToString();
    }

    // Let base class attempt other conversions
    return base.ConvertTo(context, culture, value, destinationType);
  }  
}

ExpandableObjectConverterから継承していることに注意してください。これは、AgeWrapperクラスにAgeWrapper.Ageという子プロパティがあり、グリッドのAgeWrapperエントリの横に+ボタンを配置して公開する必要があるためです。クラスに公開したい子プロパティがない場合は、代わりにTypeConverterから継承します。次に、このコンバーターをクラスに適用します...

[TypeConverter(typeof(AgeWrapperConverter))]
public class AgeWrapper
于 2008-10-28T01:14:40.970 に答える