C# WPF属性网格

C# WPF属性网格,c#,wpf,controls,properties,propertygrid,C#,Wpf,Controls,Properties,Propertygrid,当绑定到依赖对象类时,是否有一种方法隐藏类属性? 我是说那些“Dispatcher”、“DependencyObjectType”和“IsSealed” Jesse,你的问题不是很清楚,但如果我理解正确,你只想显示SampleObject中存在的属性,即由你添加的属性,而不是从其基类继承的属性 1。检查PropertyGrid如何获取选定对象的属性- 它应该获取选定对象类型的属性,而不是对象本身的属性- Type type = this.SelectedItem.GetType(); prop

当绑定到依赖对象类时,是否有一种方法隐藏类属性? 我是说那些“Dispatcher”、“DependencyObjectType”和“IsSealed”


Jesse,你的问题不是很清楚,但如果我理解正确,你只想显示
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;
    }
}

Jesse,你的问题不是很清楚,但如果我理解正确,你只想显示
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;
    }
}

这适用于作为依赖项对象的可扩展对象。我刚刚创建了一个名为ViewablePropertyAttribute的简单标记属性。我不希望所有依赖项对象都在网格中可用

    public class EllevateExpandableObjectConverter : ExpandableObjectConverter
{
    public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext   context, object value, Attribute[] attributes)
    {
        var propertyDescriptors =  
            base.GetProperties(context, value, attributes.OfType<ViewablePropertyAttribute>().Cast<Attribute>().ToArray());

        var result = propertyDescriptors.Cast<PropertyDescriptor>()
            .Where(pd => pd.Attributes.OfType<ViewablePropertyAttribute>().Any())
            .ToArray();

        return new PropertyDescriptorCollection(result);
    }
}

[ViewablePropertyAttribute]
[TypeConverter(typeof(EllevateExpandableObjectConverter)]
public MyComplexType MyInstance {get;set; }
公共类EllevateeExpandableObjectConverter:ExpandableObjectConverter
{
公共重写属性描述或集合GetProperties(ITypeDescriptorContext上下文、对象值、属性[]属性)
{
变量propertyDescriptors=
base.GetProperties(context、value、attributes.OfType().Cast().ToArray());
var result=propertyDescriptors.Cast()
.Where(pd=>pd.Attributes.OfType().Any())
.ToArray();
返回新的PropertyDescriptorCollection(结果);
}
}
[可视属性属性]
[类型转换器(类型化(EllevateeExpandableObjectConverter)]
公共MyComplexType MyInstance{get;set;}

这适用于作为依赖项对象的可扩展对象。我刚刚创建了一个名为ViewablePropertyAttribute的简单标记属性。我不希望所有依赖项对象都在网格中可用

    public class EllevateExpandableObjectConverter : ExpandableObjectConverter
{
    public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext   context, object value, Attribute[] attributes)
    {
        var propertyDescriptors =  
            base.GetProperties(context, value, attributes.OfType<ViewablePropertyAttribute>().Cast<Attribute>().ToArray());

        var result = propertyDescriptors.Cast<PropertyDescriptor>()
            .Where(pd => pd.Attributes.OfType<ViewablePropertyAttribute>().Any())
            .ToArray();

        return new PropertyDescriptorCollection(result);
    }
}

[ViewablePropertyAttribute]
[TypeConverter(typeof(EllevateExpandableObjectConverter)]
public MyComplexType MyInstance {get;set; }
公共类EllevateeExpandableObjectConverter:ExpandableObjectConverter
{
公共重写属性描述或集合GetProperties(ITypeDescriptorContext上下文、对象值、属性[]属性)
{
变量propertyDescriptors=
base.GetProperties(context、value、attributes.OfType().Cast().ToArray());
var result=propertyDescriptors.Cast()
.Where(pd=>pd.Attributes.OfType().Any())
.ToArray();
返回新的PropertyDescriptorCollection(结果);
}
}
[可视属性属性]
[类型转换器(类型化(EllevateeExpandableObjectConverter)]
公共MyComplexType MyInstance{get;set;}