Sorting based on sortorder for mutliple PG selected Objects.

Grids for WPF Forum

Posted 13 years ago by Ong Chuan Eng

Currently I have applied custom sort to the property Grid using SortOrder Attibute priority.
Custom sort works fine only when single property grid object is selected.

How to retain the same custom sort order when multiple property grid objects selected?

Thanks in advance.

Comments (1)

Posted 13 years ago by Actipro Software Support - Cleveland, OH, USA

When you have multiple objects their common properties (where their name and type are the same) are merged. In this case you could have two different sort orders for the "same" property. Assuming that this is not a problem in you case and that you used our Sort w/ Custom Attribute QuickStart, you could update the SortOrderComparer.GetSortOrder method to something like the following:
/// <summary>
/// Gets the sort order of the specified property.
/// </summary>
/// <param name="obj">The property whose sort order should be returned.</param>
/// <returns>The sort order of the specified property.</returns>
private static int GetSortOrder(object obj) {
    CustomSortCategoryDataAccessor categoryAccessor = obj as CustomSortCategoryDataAccessor;
    if (categoryAccessor != null)
        return categoryAccessor.SortPriority;

    PropertyDescriptorDataAccessorBase propertyAccessor = obj as PropertyDescriptorDataAccessorBase;
    if (propertyAccessor == null) {
        // Determine if this is a merged property and if so then get the sort order from the first property
        MergedPropertyDataAccessor mergedPropertyAccessor = obj as MergedPropertyDataAccessor;
        if (mergedPropertyAccessor != null && mergedPropertyAccessor.PropertyDataAccessors.Count != 0)
            propertyAccessor = mergedPropertyAccessor.PropertyDataAccessors[0] as PropertyDescriptorDataAccessorBase;

    if (propertyAccessor != null && propertyAccessor.PropertyDescriptor != null) {
        SortOrderAttribute sortOrder = propertyAccessor.PropertyDescriptor.Attributes[typeof(SortOrderAttribute)] as SortOrderAttribute;
        if (sortOrder != null)
            return sortOrder.Priority;

    return 0;
In the code above, we use the SortOrderAttribute of the first merged property. You may want to use different logic to resolve which SortOrderAttribute to use.

This will also be included in the sample for the next maintenance release.

Actipro Software Support

The latest build of this product (v24.1.2) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.