2010-12-06 11 views
1

종속성 개체 클래스에 바인딩 할 때 클래스 속성을 숨기는 방법이 있습니까? "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 즉에 존재하는 속성의 기본 클래스에서 상속 된 것들 귀하가 아니라 추가 사람들을 보여주고 싶은 이해합니다. 당신의 PropertyGrid가 선택한 개체의 속성을 가져 오는 방법

1 체크 -

이 객체 자체에 대한 선택한 개체 유형의 특성과하지를 가져해야 - 대신

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