2012-07-16 148 views
0

我有一個wpf數據網格,我想按編程方式排序,就好像用戶點擊了標題。經過一番搜索,我發現使用這個參考:爲什麼設置datagrid.Items.SortDescriptions(#)。給出錯誤的方向?

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

看起來像它會工作。 Intellisense說方向是一個getter和setter,但是當我嘗試將它分配給某個東西時,我會得到「表達式是一個值,因此不能作爲賦值的目標」錯誤。作爲一個二傳手,我應該能夠把它分配給一個值,對嗎?任何想法出了什麼問題?

回答

1

它看起來好像SortDescriptions是裝箱值。

請改爲嘗試以下操作。

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

不幸的是,sortdescription是..我忘了它的術語,但一旦它被分配它不能被重新分配,所以它不能這樣做。 sortDescription將不得不被創建爲一個新的對象。但是這指出了我朝着正確的方向,謝謝! – cost 2012-07-17 18:07:55

+0

冷凍......... – 2012-07-17 19:09:44

相關問題