expandable collectiondisplaymode extension

Grids for WPF Forum

Posted 8 years ago by Mary Fontana - Rudolph Technologies
Avatar
I am using the expandable collection display mode to display a property that is a list of 7 booleans(that represent selection of the days of the week).
If I change this List<boolean> to a Dictionary<string,boolean>(7) (with keys "Mon", "Tue", etc) the keys do not expand in the order that I added them to the dictionary.

If I keep the property as List<boolean> I would like to change the name field of [0] to "Mon" [1] to "Tue" etc. Can I do this?

Thanks for your help.
Mary

Comments (1)

Posted 8 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Mary,

The items in an expanded property are sorted using the same rules as all the other items. So in your case, you'd probably want to disable sorting for that particular property.

If you want to turn of sorting altogether, you can set PropertyGrid.AreDefaultSortDescriptionsEnabled to false.

If you want to turn of sorting on a per-item basis, you can use an implicit Style for the PropertyGridDataAccessorItem type to set PropertyGridDataAccessorItem.AreSortDescriptionsInherited to false. You'd want to use a trigger where DataAcessorType is Property and ValueName is the name of the property whose items you don't want sorted. There's an example of this type of Style here.

Finally, you can customize the "[0]" also. In this case, you'd need to create a custom TypeConverter for your collection property. By default, when you use the Expandable collection display mode, a default instance of our ExpandableCollectionConverter will be used. This displays lists using "[0]", "[1]", etc.

Luckily our ExpandableCollectionConverter is extensible, so you create a custom class that derives from it to alter how your collection is displayed. Something like the following should work. Once you add that, you'd associate it with your property using TypeConverterAttribute.
public class MyExpandableCollectionConverter : ExpandableCollectionConverter {

    // This nested class "describes" the properties
    protected class MyCollectionPropertyDescriptor : ExpandableCollectionConverter.CollectionPropertyDescriptor {
        public MyCollectionPropertyDescriptor(ICollection collection, int index, object item, Type itemType, Attribute[] attributes)
            : base(collection, index, item, itemType, attributes) {
            // No-op
        }

        public override string DisplayName {
            get {
                // TODO: Return custom string like "Mon", "Tue"
                return base.DisplayName;
            }
        }
    }

    // Inject our custom property descriptor
    protected override PropertyDescriptor CreateCollectionPropertyDescriptor(ITypeDescriptorContext context, Attribute[] attributes, ICollection collection, int index, object item, Type itemType) {
        return new MyCollectionPropertyDescriptor(collection, index, item, itemType, null);
    }
}


Actipro Software Support

The latest build of this product (v2018.1 build 0675) was released 10 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.