tags:

views:

433

answers:

1

There is a strange bug in ListView/GridView in WPF when the SelectionMode is set to Single.

Ways to reproduce:

Generate a collection (the collection must have enough items so that the ListView can at least scroll 2-3 pages):

var customers = from c in _db.Customers
    orderby c.Name, c.City
     select c;

Bind the collection to the ListView:

dataGrid.ItemsSource = customers.ToList();

On the first page, change your selection 3-4 items. Remember which items you previously selected. Scroll down using mouse wheel, so that you are on the next page. Scroll back up. Voila!! You will see all the items you clicked selected?! Image of this error: http://img261.imageshack.us/img261/133/listview.jpg

This same issue plagues Wpf toolkit's datagrid too.

Even stranger is that every selection is appended to SelectedItems property. So if you changed your selection 10 times, you will have 10 items in SelectedItems property with the current selection as the last item.

Can somebody tell me why this is happening? Is this intended or a bug? Seems more like a bug to me.

Somebody else too encountered this bug. Old article, but the bug still remains: http://cs.blueberryislandmedia.com/blogs/blueberries/archive/2009/04/24/bug-in-wpf-listview-single-selection-mode.aspx

+3  A: 

This is interesting and could be a bug. It is such a common control and scenario, though, I suspect something else could be going on. I found one reference to something that looks similar to this problem here. The advice given to solve it follows:

If you're overriding Equals in an object that's being displayed in a ListView, do it right Otherwise, you'll get all sorts of interesting behavior...

I guess the theory here is that somehow a bug in Equals will throw off the logic in the ListView. Please verify you are not overriding Equals and post your results. If you are not, and you don't get any other help here, I recommend you file this as a bug on Microsoft Connect.

Jerry Bullard
Wow. It fixed the issue. Thanks a lot. :)
Yogesh
I cannot upvote it because of "Vote too old to be changed, unless this answer is edited" thing. The vote update window is really small.
Yogesh