WPF属性网格

时间:2010-12-06 13:53:17

标签: c# wpf controls properties propertygrid

绑定到Dependency Object类时,有没有办法隐藏类属性? 我的意思是“Dispatcher”,“DependencyObjectType”和“IsSealed”?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows;

namespace WPGDemoApp
{

   public class SampleObject : DependencyObject
   {
      readonly static DependencyProperty IdProperty = DependencyProperty.Register("ID", typeof(int), typeof(SampleObject));


      public int ID
      {
         get { return (int)GetValue(IdProperty); }
         set
         {
            SetValue(IdProperty, value);
         }
      }
      public string Name
      {
         get { return "Leeroy Jenkins"; }
      }
   }
}

2 个答案:

答案 0 :(得分:1)

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;
    }
}

在属性网格中,在将属性添加到网格之前添加一个检查。像这样的东西 -

// 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);
}

答案 1 :(得分:0)

这适用于作为依赖项对象的可扩展对象。我刚刚创建了一个名为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; }