Order in Categories & Properties

Grids for WPF Forum

Posted 9 years ago by Eli Obadia
Version: 10.1.0523
Avatar
How can I set a specific order in the way the Categories are displayed and also in the order properties are displayed inside a category?

Is there anything such as [Category("System",1)] for the categories or
[Order(1)] for teh properties?

Thanks,
Eli

Comments (7)

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

Using attributes on the properties to specify the sort order is not something that is supported out of the box, but it is possible. The main issue is that the .NET framework does not have a SortOrderAttribute or anything like it. We could create our own one, but most customers would not want to reference our PropertyGrid assembly from the data assemblies.

I've marked down a TODO item to add a QuickStart that shows how this can be done.

Until then, you can build your own solution using a custom DataFactory. This post shows how one customer build such a solution. This only sorts properties, but not categories.

The problem with categories is the category sort order attributes are associated with properties. There's no actual "category" to which attributes can be attached. So having a "Category("System" 1)" defined on the property doesn't really work. You could have several properties with the same category name, but different sort order numbers.

Probably the most straight forward way to solve this would be to define "Category("System", 1)" on the class definition. Then you'd sort the categories in your custom factory, in a GetDataAccessors(object[], DataFactoryOptions) override. Like the other forum post, you'd call the base class's method, then just sort the results.


Actipro Software Support

Posted 9 years ago by Eli Obadia
Avatar
I tried to understand that post, but "no idea" how to do it, do you think you'll have soon that sample code?

Thansk!
Eli
Posted 9 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Eli,

We are currently focusing on the next WPF Studio release, so it might be some time before we get to this sample. The basic steps are:

1. Create a SortOrderAttribute class that has an Int32 property
2. Create the custom DataFactory like described in the other forum post
3. Set the PropertyGrid.DataFactory to a new instance of your custom DataFactory.

Then you can use your SortOrderAttribute on any properties.

If this is something you need immediately, you can use our consulting services to bump up the priority. This was worked out well for customers in the past, when their priorities don't align with ours. You can find more information and make a request here.


Actipro Software Support

Posted 9 years ago by Eli Obadia
Avatar
How can I create a new Attribute? (I guess that this mean adding another "field" to the Property object, no?)

Thanks,
Eli
Posted 9 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Eli,

An attribute is just metadata that can be added to classes, properties, methods, etc. Anything that derives from the Attribute class is an attribute. You can use Reflector to see how some of the common attributes are defined (i.e. BrowsableAttribute, DisplayNameAttribute). Once you create your attribute, you can decorate your properties with it like any other attribute. Then in your custom DataFactory you can access that attribute and use it to sort the items.

This MSDN article includes more information.


Actipro Software Support

Posted 9 years ago by Eli Obadia
Avatar
Thanks,
After I have craeted my Attribute (e.g position) and assigned to a specific class (which is the class of one of the properties of the propertygrid) (e.g. Name), how can I get the value of the property in the CustomFilter Class that I created for my propertygrid? can I get it from the dataAccessor parameter in the Filer code?

Can I change the Value of the Attribute at run time?

Thanks,
Eli
Posted 9 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Eli,

DataFilters are passed the IDataAccessor, which you can try to cast to an IPropertyDataAccessor. In general, you can also try to cast this to a PropertyDescriptorDataAccessorBase. If you are setting PropertyGrid.SelectedObjects to more than 1 object, then this won't work, as MergedPropertyDataAccessor is used in that case.

Your first option is to cast to IPropertyDataAccessor. In which case, you'd have to use .NET reflection (as shown in the post referenced earlier) using the Target and ValueName properties. If the property is merged (i.e. it's a MergedPropertyDataAccessor), then Target will be an array of objects, otherwise it's the object that contains the given property.

The other option is to cast to PropertyDescriptorDataAccessorBase. In this case, the PropertyDescriptor is exposed and you can get custom attributes using that (i.e. PropertyDescriptor.Attributes). Again, if it's a merged property you'd have to do some more work and try to cast to a MergedPropertyDataAccessor. Then the merged properties are available via the MergedPropertyDataAccessor.PropertyDataAccessors collection.

If you declare the attribute in your source code, then you cannot change it during runtime. You can inject custom attributes dynamically using things like ICustomTypeDescriptor. But you'd have to call PropertyGrid.Refresh to pick up the new sort values.

Also, you can apply sorting using a custom IComparer, by setting PropertyGrid.SortComparer. This will be passed the IDataAccessors as well, but again you'd have to call PropertyGrid.Refresh if you change the sort order.


Actipro Software Support

The latest build of this product (v2019.1 build 0683) was released 14 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.