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.


Need Your Help

Got an import error all of a sudden

python django git virtualenv

So I set up a virtualenv on Ubuntu, installed everything, and got the development site running with runserver. All my own unit tests passed, but many of the Django packages' unit tests were not pas...

How to validate Form in MVC 2 using Entity Framework

asp.net-mvc validation

I am using MVC 2 and following the example here: Using Data Annotation Validators with the Entity Framework

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.