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
|
Hi, Please see attached example project. Here I have use a TypeConverter in order to sort the Properties that I have defined in MyClass. When I create a single instance and associate it with propGrid.SelectedObject all is fine and properties are in the order I specified. However when I have an array/list of instances and associate it with propGrid.SelectedObjects a crash will happen. I have investigated a little. When all properties are of type string, then there is no problem. With 1 string and 1 int, nothing gets displayed. With an additional boolean it will crash. Looks like some casting does not succeeed in the Many<T> class. Best regards, Jan van de Pol |
|
|
Thanks for letting us know about this. It turns out this is a very strange bug in the .NET Framework, specifically with the PropertyDescriptorCollection.Sort method which your PropertySorter class calls. As far as I can tell this kind of marks the collection for sorting but doesn't actually sort it until it is iterated (so when we perform lookups into the collection, it returns incorrect results). To work around this, replace the last line of PropertySorter.GetProperties with the following: var sorted = pdc.Sort((string[]) propertyNames.ToArray(typeof (string))); Note the empty foreach loop after the Sort call: this seems to be what it takes to get the sort to "take." If you find this ugly (and who wouldn't!) then doing a CopyTo into a PropertyDescriptor[] array and returning a new PropertyDescriptorCollection(array, true) also works but is a bit more verbose. |
|
|
Thanks Ivan,
Your workaround works perfectly. But since we have LINQ nowadays I thought, let's rewrite the whole GetProperties method like so:
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute [] attributes) // Sort the PropertyDescriptors using a LINQ expression //
Much simpler and it does the trick. Thanks again for pointing me in the right direction. Best regards,
Jan
|
|