2010-12-06 55 views
1

有沒有辦法在綁定到Dependency Object類時隱藏類屬性? 我的意思是那些「Dispatcher」,「DependencyObjectType」和「IsSealed」?WPF Property Grid

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

回答

1

傑西,你的問題不是很清楚,但如果我理解它正確,你只是想表明出現在您的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); 
} 
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; }