2013-04-25 188 views
1

我正在为我的WPF项目寻找一个PropertyGrid,使我可以自定义列出的属性/类别的顺序。现在我正在使用Extended WPF Toolkit s(Community Edition)PropertyGrid和CustomPropertyDescriptor s。我的研究表明,使用PropertyGrid进行自定义排序是不可能的。WPF Propertygrid与自定义排序

有没有(最好是免费的)解决方案?

+0

什么?我用不到一个小时的时间创建了自己的Property Grid,其中包含大约20行XAML和30行C#代码。我不知道你在说什么。 – 2013-04-26 14:55:24

+0

幸运啦!任何提示如何开始?你使用DataGrid作为基础吗? – mamuesstack 2013-04-29 06:02:00

回答

1

通过使用PropertyOrderAttribute属性修饰属性,可以实现扩展WPF工具包中属性的排序。

如果您不想通过在设计时用属性装饰POCO,或者订单是以某种方式动态的,那么可以在运行时通过创建类型转换器并覆盖GetProperties来添加属性方法。例如,如果你希望保持一个通用的IList类型的索引顺序:

using Xceed.Wpf.Toolkit.PropertyGrid.Attributes; 
using System.ComponentModel; 

public class MyExpandableIListConverter<T> : ExpandableObjectConverter 
{ 
    public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes) 
    { 
     if (value is IList<T>) 
     { 
      IList<T> list = value as IList<T>; 
      PropertyDescriptorCollection propDescriptions = new PropertyDescriptorCollection(null); 
      IEnumerator enumerator = list.GetEnumerator(); 
      int counter = -1; 
      while (enumerator.MoveNext()) 
      { 
       counter++; 
       propDescriptions.Add(new ListItemPropertyDescriptor<T>(list, counter)); 

      } 
      return propDescriptions; 
     } 
     else 
     { 
      return base.GetProperties(context, value, attributes); 
     } 
    }   
} 

随着ListItemPropertyDescriptor被定义如下:

using Xceed.Wpf.Toolkit.PropertyGrid.Attributes; 
using System.ComponentModel; 

public class ListItemPropertyDescriptor<T> : PropertyDescriptor 
{ 
    private readonly IList<T> owner; 
    private readonly int index; 

    public ListItemPropertyDescriptor(IList<T> owner, int index) : base("["+ index+"]", null) 
    { 
     this.owner = owner; 
     this.index = index; 

    } 

    public override AttributeCollection Attributes 
    { 
     get 
     { 
      var attributes = TypeDescriptor.GetAttributes(GetValue(null), false); 
      //If the Xceed expandable object attribute is not applied then apply it 
      if (!attributes.OfType<ExpandableObjectAttribute>().Any()) 
      { 
       attributes = AddAttribute(new ExpandableObjectAttribute(), attributes); 
      } 

      //set the xceed order attribute 
      attributes = AddAttribute(new PropertyOrderAttribute(index), attributes); 

      return attributes; 
     } 
    } 
    private AttributeCollection AddAttribute(Attribute newAttribute, AttributeCollection oldAttributes) 
    { 
     Attribute[] newAttributes = new Attribute[oldAttributes.Count + 1]; 
     oldAttributes.CopyTo(newAttributes, 1); 
     newAttributes[0] = newAttribute; 

     return new AttributeCollection(newAttributes); 
    } 

    public override bool CanResetValue(object component) 
    { 
     return false; 
    } 

    public override object GetValue(object component) 
    { 
     return Value; 
    } 

    private T Value 
     => owner[index]; 

    public override void ResetValue(object component) 
    { 
     throw new NotImplementedException(); 
    } 

    public override void SetValue(object component, object value) 
    { 
     owner[index] = (T)value; 
    } 

    public override bool ShouldSerializeValue(object component) 
    { 
     return false; 
    } 

    public override Type ComponentType 
     => owner.GetType(); 

    public override bool IsReadOnly 
     => false; 

    public override Type PropertyType 
     => Value?.GetType(); 

} 

此部分代码是从following SO answer

适于