제시, 귀하의 질문에 매우 명확하지하지만 난 제대로 당신이 당신 SampleObject
즉에 존재하는 속성의 기본 클래스에서 상속 된 것들 귀하가 아니라 추가 사람들을 보여주고 싶은 이해합니다. 당신의 PropertyGrid가 선택한 개체의 속성을 가져 오는 방법
1 체크 -
이 객체 자체에 대한 선택한 개체 유형의 특성과하지를 가져해야 - 대신
Type type = this.SelectedItem.GetType();
properties =TypeDescriptor.GetProperties(type);
-
properties = TypeDescriptor.GetProperties(this.SelectedItem);
2이 프로가 해결되지 않으면 또는 PG에 표시 할 속성을 더 많이 제어하려는 경우 사용자 지정 특성을 만들 수 있습니다. 이를 달성하기 위해 사용자 정의 속성 (IsBrowsable
과 유사)을 만들었습니다. 속성을 사용하여 속성을 꾸미고 속성 그리드 구현을 수정해야합니다. 그리드에 속성을 추가하기 전에 검사를 추가, 재산 그리드에서
/// <summary>
/// Attribute to identify the Custom Proeprties.
/// Only Proeprties marked with this attribute(true) will be displayed in property grid.
/// </summary>
[global::System.AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
public sealed class IsCustomPropertyAttribute : Attribute
{
// See the attribute guidelines at
// http://go.microsoft.com/fwlink/?LinkId=85236
private bool isCustomProperty;
public static readonly IsCustomPropertyAttribute Default = new IsCustomPropertyAttribute(false);
public static readonly IsCustomPropertyAttribute No = new IsCustomPropertyAttribute(false);
public static readonly IsCustomPropertyAttribute Yes = new IsCustomPropertyAttribute(true);
/// <summary>
/// Initializes a new instance of the <see cref="IsCustomPropertyAttribute"/> class.
/// </summary>
/// <param name="isCustomProperty">if set to <c>true</c> [is RT display property].</param>
public IsCustomPropertyAttribute(bool isCustomProperty)
{
this.isCustomProperty = isCustomProperty;
}
/// <summary>
/// Gets a value indicating whether this instance is RT display property.
/// </summary>
/// <value>
/// <c>true</c> if this instance is RT display property; otherwise, <c>false</c>.
/// The default is false.
/// </value>
public bool IsCustomProperty
{
get { return isCustomProperty; }
set { isCustomProperty = value; }
}
/// <summary>
/// Determines whether the specified <see cref="System.Object"/> is equal to this instance.
/// </summary>
/// <param name="obj">The <see cref="System.Object"/> to compare with this instance.</param>
/// <returns>
/// <c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.
/// </returns>
public override bool Equals(object obj)
{
IsCustomPropertyAttribute attribute = obj as IsCustomPropertyAttribute;
if (obj == null)
return false;
if (obj == this)
return true;
return attribute.isCustomProperty == isCustomProperty;
}
public override int GetHashCode()
{
return isCustomProperty.GetHashCode();
}
public override bool IsDefaultAttribute()
{
return isCustomProperty == IsCustomPropertyAttribute.Default.isCustomProperty;
}
}
- 여기
는 속성 클래스입니다. 이런 식으로 -
// Gets the attributes for the property.
AttributeCollection attributes = propertyDescriptor.Attributes;
//Checks to see if the value of the IsCustomPropertyAttribute is Yes.
IsCustomPropertyAttribute myAttribute =
(IsCustomPropertyAttribute)attributes[typeof(IsCustomPropertyAttribute)];
//Check if current property is CustomProperty or not
if (myAttribute.IsCustomProperty == true)
{
AddProperty(propertyDescriptor);
}