This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Hello, How can I control the order in which the Categories are presented in the propertygrid? Thanks Oscar |
|
|
Hello, Just checking on the status of the question. Thanks Oscar |
|
|
Hi Oscar, The easiest way to do this is to create a class that implements IComparer to sort PropertyGridRow objects by their category. Here is a simple example of such a class that sorts alphabetically:
Then set the Sorting property of the PropertyGrid to be an instance of this comparer. Hope that helps. Let me know if you have further questions about this. -Jason Fauchelle |
|
|
Thanks Jason for your response. It's a bit closer to what I was looking for but not quite. It's my fault I wasn't specific enough. I would like to sort the Categories however I would like to leave the properties in the order in which they were specified in the class. With is approach both the Categories and Properties are sorted. Thanks Oscar |
|
|
Hi Oscar, Certainly, I had a feeling this was the behavior that you'd want. From what I'm seeing here, the comparer implementation that I provided is sorting all the properties by their category, but within each category, the properties are kept in the same order as they appear in the class. This behaviour is provided by WPF - when sorting multiple objects using the same value (e.g. they all have the same category), then the order of the objects remain the same as when they were passed through the comparer. Note that the sorting and grouping logic is provided by a WPF class called CollectionViewSource. I don't have too much experience with it, but I don't think there is a way to tell it to sort just by the groups. The collection contains a single type of item (in our case PropertyGridRow) and the groups are abstracted from those objects based on some criteria (in our case the category name). Applying sorting is going to be applied to all the objects in the collection. But by sorting by the same criteria as the grouping, it should produce the effect of sorted groups, but keep all the items in their original order within their groups. If you can provide a simple repro project, I can look into this better for you. -Jason Fauchelle |
|
|
Hi Jason, I have tried but as yet have been unsuccessful in creating a repro project. In the repro project the ordering is fine. However the ordering does change in my main application but not sure why. I have been able to fix it by waiting for propertygrid Loaded event at which point I add the code
I'm using content controls and datatemplates to dynamically load different usercontrols depending on what the user selects on a treeview which results in selecting a viewmodel. Some of these datatemplates contain the propertygrid. Not sure what is happening here? Thanks for your help. I'll let you know if I can reproduce the issue. Thanks Oscar |
|
|
Hi Oscar, No sorry, I have no idea what is happening here. It's great that you found a working solution though - odd that waiting for the property grid to load would affect the ordering of properties. Do let me know if you reproduce the issue. -Jason Fauchelle |
|
|
Hi Jason, I have been able to reproduce the issue. Please find the attached repro project. I tried to keep it as simple as possible but still show the issue. When you run the project you will see that there are three propertygrids. The first propertygrid does not have a Comparer however the second one does have a comparer. This "comparer" is used to sort the Categories. You will note that if you select different items from the dropdown on the property named "Prior Distribution Funtion" the child properties change. Though the order of the child properties are the same when first loaded if you go back and forth selecting different items from the dropdown the order of the child properties change and are different. For example for "Uniform(1.1, 5.1)" you will see first "Minimum" and then "Maximum" in the first propertygrid and this is the correct order we want since it is the order the properties were defined in the class. But in the second propertygrid which uses the comparer we get first "Maximum" and then "Minimum". It would be really helpful if you are able to find a solution as unfortunately my previous work around does not help in this case. Thanks Oscar |
|
|
Hi Oscar, Thanks for sending through the repro project. I will look into this for you soon. -Jason Fauchelle |
|
|
Hi Oscar, I've found that you can work around this issue by setting SortSubproperties of the PropertyGrid to false. This will cause properties to always be displayed in their defined order. This should work within categories as well. I wasn't able to find the cause of the incorrect sorting. Everything is fine up until we pass the values and sort comparer to the built in WPF ListCollectionView. Hope the work around solves the problem. -Jason Fauchelle |
|