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.
我有一个带有绑定项源的数据网格。我已将datagrid的CanUserSortColumns属性设置为TRUE,而datagrid中的所有内部列也是如此,但用户仍然无法对列进行排序。
Is there something I have missed ?
有没有我错过的东西?
2 个解决方案
#1
3
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 -
您是否为标题明确定义了DataTemplate?如果是,则必须将“SortMemberPath”列上的属性设置为要对列进行排序的CLR属性。这个链接可能对你有所帮助,看看它 -
WPF4 Datagrid doesn't sort on column headers
WPF4 Datagrid不对列标题进行排序
#2
1
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.
这些列的类型必须实现非泛型IComparable,如果使用原始或.net类型,这通常不是问题。但是如果你有自己的类型,那么你将不得不添加它。
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); }
}
#1
3
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 -
您是否为标题明确定义了DataTemplate?如果是,则必须将“SortMemberPath”列上的属性设置为要对列进行排序的CLR属性。这个链接可能对你有所帮助,看看它 -
WPF4 Datagrid doesn't sort on column headers
WPF4 Datagrid不对列标题进行排序
#2
1
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.
这些列的类型必须实现非泛型IComparable,如果使用原始或.net类型,这通常不是问题。但是如果你有自己的类型,那么你将不得不添加它。
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); }
}