Question

Unable to sort items with custom TypeDescriptorFactory

Posted 9 years ago by Avatar Michael Hays - Owner, Spiral LLC
I would like to have very granular control over sorting. So I've created a custom attribute called SortRank, which takes an integer. I realize that the documentation says that SortImportance is considered, and then DisplayName, but under the help, under "Building a Custom Factory", there was this glimmer of hope:

"By deriving from TypeDescriptorFactory or TypeReflectionFactory, the custom factory can customize the items or properties already generated by these factories. Such as adding or removing properties, categories, or category editors, or rearranging the items."

Is it possible to sort like this? If so, what am I doing wrong? I am including my derived class -- it's very simple:

---------------------------------------------------

public class ExtendedTypeDescriptorFactory : TypeDescriptorFactory
{
protected override IList<IPropertyDataAccessor> GetProperties(object value, DataFactoryOptions options)
{
var p = base.GetProperties(value, options);

if (p == null) return null;

var result = new List<IPropertyDataAccessor>(p.OrderBy(prop =>
{
var rank =
(Window1.SortOrderAttribute)
value.GetType().GetProperty(prop.ValueName).GetCustomAttributes(false).
FirstOrDefault(x => x.GetType().Equals(typeof (Window1.SortRankAttribute)));
return (rank == null) ? 0 : rank.Rank;
}));

return result;
}
}

Comments (6)

Posted 9 years ago by Actipro Software Support - Cleveland, OH, USA
Hi Micheal,

You should clear the PropertyGrid.SortDescriptions collection. Otherwise, it would sort the properties (using it's settings) after your sorting has been applied. We are using a WPF CollectionView to sort things for us, which uses an unstable sort. This basically means that run the a sort against the same list, may result in a different order (albeit one that adheres to the sort description).

Another possbile problem is that you are sorting in the GetProperties(object, DataFactoryOptions) method. The properties returned by this method must still be merged (in the case where SelectedObjects has several objects) and/or categorized. These happen in GetProperties(object[], DataFactoryOptions) and GetAccessors(object[], DataFactoryOptions), respectively. So you may need to apply your sorting in one of those methods instead.

Actipro Software Support
Posted 9 years ago by Michael Hays - Owner, Spiral LLC
Thank you so much for your help. Of course you were exactly right on both point. I moved my sort routine to GetProperties(object[]...) and cleared the SortDescriptions collection. Actually, I just needed to remove "DisplayName". Worked great.

Thanks again.
Posted 7 years ago by Actipro Software Support - Cleveland, OH, USA
Hello,

FYI, we've added a QuickStart that shows how to define and use a custom SortOrderAttribute and CategorySortOrderAttribute called "Sorting w/ Custom Attribute QuickStart".

Actipro Software Support
Posted 5 months ago by Procam

Hello,

after the masive update with v17 the SortOrderAttribute and CategorySortOrderAttribute stopped to work. The items in categpries are not sorted as deifned in model classes, e.g. when setting in code:

[Category("Position"), SortOrder(1)]
public double Top
{

}

this property is displayed not in the given position, the same happens with all the another properties. Can you please tell me how to fix this?

The SortOrderAttribute implementation is the same as you provided in in v16:

\Actipro Software\WPF-Controls\v16.1.0633\SampleBrowser\ProductSamples\PropertyGridSamples\QuickStart\SortingCustomAttribute\SortOrderAttribute.cs

Thanks for your answer!

[Modified 5 months ago]

Posted 5 months ago by Procam

I think I can answer myself :)

Now it should be:

[Category("Position")]
[Display(Order = 1)]
public double Top
{

}

 

If I am right then I think I can safelly remove the file SortOrderAttribute.cs from my project, yes?

Posted 5 months ago by Actipro Software Support - Cleveland, OH, USA

Hello,

Yes you've got it, and can remove the older SortOrderAttribute.  The "Supported Property Attributes" documentation topic talks about this a bit.  Under the "DisplayAttribute" portion (which is standard across WPF/UWP), we have:

Provides a general-purpose attribute that lets you specify sort order and localizable strings for category, display name, description.


Actipro Software Support
Information The latest build of this product (2018.1 build 0673) was released 29 days ago, which was after the last post in this thread.

Add a Comment

Please log in to a validated account to post comments.