Is there an easy way to reorder items in a ListView
I have a ListView data bound to an ObservableCollection. I set the CanReorderItems property to true.
However, when I drag items in the listview, they stay in their original position.
Is there any easy way to enable reordering in a list view?
Here is my code: ctor of page:
public BlankPage() { this.InitializeComponent(); ObservableCollection<MyClass> c = new ObservableCollection<MyClass>(); c.Add(new MyClass("a")); c.Add(new MyClass("b")); c.Add(new MyClass("c")); DataContext = c; }
XAML of page:
<ListView ItemsSource="{Binding}" CanDragItems="true" CanReorderItems="true" AllowDrop="True"></ListView>
Answers
There is a bug in ListView and GridView now (note the question was asked before Windows 8.0 released) and you need to use a CollectionViewSource:
public BlankPage() { this.InitializeComponent(); ObservableCollection<MyClass> c = new ObservableCollection<MyClass>(); c.Add(new MyClass("a")); c.Add(new MyClass("b")); c.Add(new MyClass("c")); var viewSource = new CollectionViewSource { Source = c }; DataContext = viewSource.View; }
You need to turn on AllowDrop and have your ItemsSource be an ObservableCollection or ICollectionViewSource.