Jesse, Ihre Frage ist nicht ganz klar, aber wenn ich verstehe es richtig zeigen wollen Sie nur noch die aktuellen Eigenschaften in Ihrer SampleObject
das heißt jene hinzugefügt von Ihnen und nicht die, die von ihrer Basisklasse geerbt.
1. überprüfen, wie Ihre Property wird, um die Eigenschaften des ausgewählten Objekts bekommen -
es die Eigenschaften der ausgewählten Objekte Typ und nicht für das Objekt selbst holen sollte -
Type type = this.SelectedItem.GetType();
properties =TypeDescriptor.GetProperties(type);
statt -
properties = TypeDescriptor.GetProperties(this.SelectedItem);
2. Ist dies nicht der pro löst Wenn Sie mehr Kontrolle darüber haben möchten, welche Eigenschaften in Ihrem PG angezeigt werden sollen, können Sie ein benutzerdefiniertes Attribut erstellen. Um dies zu erreichen, habe ich ein benutzerdefiniertes Attribut erstellt (ähnlich IsBrowsable
), Sie müssen nur Ihre Property mit diesem Attribut dekorieren und Ihre Property Grid-Implementierung modifizieren, um sie zu respektieren. Hier
ist das Attribut Klasse -
/// <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;
}
}
In Ihrer Eigenschaft Gitter, einen Scheck hinzufügen, bevor die Eigenschaft auf Raster hinzufügen. etwas wie das -
// 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);
}