문제

내가이 대상을 묶을 때

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 항목 옆에 + 버튼을 가지고 노출되어야하는 agewrapper.age라는 자식 속성이 있기 때문입니다. 수업에 노출하려는 자식 속성이없는 경우 대신 TypeConverter에서 상속하십시오. 이제이 변환기를 수업에 적용하십시오 ...

[TypeConverter(typeof(AgeWrapperConverter))]
public class AgeWrapper
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top