为什么设置datagrid.Items.SortDescriptions(#)。方向给出错误?

时间:2012-07-16 22:30:43

标签: wpf vb.net datagrid

我有一个wpf数据网格,我想以编程方式对其进行排序,就像用户点击了标题一样。经过一番搜索,我发现了使用它的参考:

datagrid_selected.Items.SortDescriptions(2).Direction = ComponentModel.ListSortDirection.Ascending

看起来会起作用。 Intellisense说Direction是一个getter和setter,但是当我尝试将它分配给某些东西时,我得到了" Expression是一个值,因此不能成为赋值的目标"错误。作为一个setter本质上我应该能够将它赋值给一个值,对吧?知道出了什么问题吗?

1 个答案:

答案 0 :(得分:1)

看起来好像SortDescriptions是盒装值。

相反,请尝试以下方法。

var sortDescription = grid.Items.SortDescriptions[0];
sortDescription.Direction = System.ComponentModel.ListSortDirection.Ascending;
grid.Items.SortDescriptions[0] = sortDescription;