2011-07-02 42 views
3

我的應用程序中有一個可排序的ListBox,其中包含一系列具有DisplayOrder屬性的項目。這些項目最初按DisplayOrder排序,但用戶可以通過拖放對其進行重新排序。如何堅持WPF ItemsControl中項目的排序順序?

我通過使用我的SortDescription將ListBox的ItemsSource設置爲CollectionViewSource來設置初始排序順序。

拖放操作發生後,如何更新受影響項目的DisplayOrder屬性?

這是我在我的拖放操作的事件處理程序中做的事情,還是有辦法將ListBox的索引綁定到我的DisplayOrder屬性?

回答

0

我在xaml文件背後的代碼中處理了這個問題。我將事件操作傳遞給我的視圖模型,然後在代碼中執行操作,並讓INotify使用新的順序更新UI。

查看

ChangedEvent (button click/drag drop/whatever) 
{ 
    ViewModel.MoveItemToNewLocation(); 

} 

視圖模型

MoveItemToNewLocation() 
{ 
    int newLocation = myList.IndexOf(SelectedItem); 
    int oldLocation = SelectedItem.DisplayOrder; 
    UpdateDisplayOrders(oldLocation, newLocation); 
} 

private void UpdateDisplayOrders(int oldlocation, int newlocation, object myitem) 
{ 
    // Do move logic here 

}