質問
このオブジェクトをバインドするとき
public class MyObject
{
public AgeWrapper Age
{
get;
set;
}
}
public class AgeWrapper
{
public int Age
{
get;
set;
}
}
プロパティグリッドの場合、プロパティグリッドの値セクションに表示されるのは、AgeWrapperのクラス名ですが、AgeWrapper.Ageの値です
プロパティグリッドで、その複合オブジェクトのクラス名ではなく、複合オブジェクト(この場合はAgeWrapper.Age)の値を表示できるようにする方法はありますか?
解決
型コンバーターを作成し、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
所属していません StackOverflow