傑西,你的問題不是很清楚,但如果我理解它正確,你只是想表明出現在您的SampleObject
即屬性那些由你,而不是從它的基類繼承而來的增加。
1.檢查你的PropertyGrid的是如何讓所選擇的對象的屬性 -
,應該讀所選對象類型的屬性,而不是對象本身 -
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);
}