wpf propertygrid sorts array in alphabetic order when expanded. 0,1,10,11...2,20... Anyway to order it in numerical order in the propertygrid? I have tried CollectionViewSource.GetDefaultView to define a custom sort but that didn't do anything.
Asked
Active
Viewed 162 times
0
-
1Share what you tried so far. – Ayyappan Subramanian Mar 10 '15 at 19:51
-
Does this help?: http://stackoverflow.com/questions/8453721/numerical-string-sorting-in-a-listbox – bill Mar 10 '15 at 19:57
-
I think your answer is [ICustomTypeDescriptor](https://msdn.microsoft.com/en-us/library/system.componentmodel.icustomtypedescriptor%28v=vs.110%29.aspx) . [Here](http://www.codeproject.com/Articles/4448/Customized-display-of-collection-data-in-a-Propert) is a good example – Gordon True Mar 10 '15 at 20:07
-
I have tried the following but nothing has worked. 1. ListCollectionView _arrView = CollectionViewSource.GetDefaultView(TestList) as ListCollectionView; _arrView.CustomSort = new IntSorter(); 2. Implement ICustomTypeDescriptor in my collection which calls a custom property descriptor class to get property names and values. I can write a custom propertydescriptioncollection and provide a IComparer interface but that doesn't seem to work. This option is really no good for plain arrays because this option would require me to create a collection class that inherits from other classes. – Ppp Mar 11 '15 at 01:46
2 Answers
0
Use System.Linq Enumerable.OrderBy
It let you specify any comparer.
Just do your own :
public class NumericComparer: IComparer<string>
{
public int Compare(string s1, string s2)
{
if (IsNumeric(s1) && IsNumeric(s2))
{
if (Convert.ToInt32(s1) > Convert.ToInt32(s2)) return 1;
if (Convert.ToInt32(s1) < Convert.ToInt32(s2)) return -1;
if (Convert.ToInt32(s1) == Convert.ToInt32(s2)) return 0;
}
}
private static bool IsNumeric(string value)
{
try
{
int i = Convert.ToInt32(value);
return true;
}
catch (FormatException)
{
return false;
}
}
}
Example :
Enumerable.OrderBy(x => x, new NumericComparer());

C1rdec
- 1,647
- 1
- 21
- 46
-
I don't get it. How would you link this with propertygrid? I am having a problem sorting the propertygrid. Even if I sort the the items in propertygrid remain sorted in alphabetical order – Ppp Mar 11 '15 at 01:48
0
I couldn't figure out a way to sort the array in numerical order but I have created a workaround.
Step1: Create a collection that implements ICustomTypeDescriptor. Well described here http://www.codeproject.com/Articles/4448/Customized-display-of-collection-data-in-a-Propert
Step2. Use the following override for PropertyDescriptor property.
public override string DisplayName {
get {
string formatStr="D" + this.collection.Count.ToString().Length.ToString();
return"[" + index.ToString(formatStr) +"]";
}
}
This gives displaynames to collection items as [001][002]... which works when sorted alphabetically by the propertygrid.

Ppp
- 515
- 5
- 11