I have a datagrid with binding item source. I have set CanUserSortColumns property of datagrid into TRUE and so do with all inner columns in datagrid but user still doesn't be able to sort columns.
Is there something I have missed ?
Are you explicitly defining DataTemplate
for your headers? In case yes you have to set property on your column "SortMemberPath"
to your CLR property on which you want to sort your column. This link might prove helpful to you, have a look at it -
WPF4 Datagrid doesn't sort on column headers
Thanks guys. That worked. I just want to add.
The types of those columns must implement the non-generic IComparable
, which is usually not a problem if you are using primitive or .net types. But if you have your own types, then you will have to add it.
E.g.
/* this is my own type */
public struct Distance : ..., IComparable, IComparable<Distance>, ... {
...
public int CompareTo(object obj)
{
if (obj == null) { return 1; }
if (obj.GetType() != typeof(Distance)) { return 0; }
return CompareTo((Distance)obj);
}
public int CompareTo(Distance other) { return _meters.CompareTo(other._meters); }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With