开发者

WPF ListView/GridView single selection bug

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 开发者_JAVA技巧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


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.


This is an old thread with an answer but I thought I'd add my own experience.

I had a similiar problem, multiple rows were getting selected even though the SelectionMode of my ListView was single. The behavior was very sporadic and was not related to items in the collection being equal to each other.

My ListView had the ItemsSource bound to a ObservableCollection collection.

I found that this behavior was only present when I had 5000+ items in my collection. I fixed the issue by creating a temporary collection and then setting bound collection. This reduced the updates on my ListView down to just the one update.

Maybe not the most correct solution, but it worked in my case and hopefully helps someone else.


Throwing my thoughts in on this old thread... I too was having this issue and yes deleting my custom Equals and GetHashCode overrides fixed it, but, unlike the quote in Jerry's answer, my Equals and GetHashCode override methods were indeed written correctly.

For me, the issue manifested itself when the data that was selected was changed. While the change showed up correctly in the ListView, it remained selected after that.

While I can't find a good explanation for this, my personal speculation is that the code in ListView may be assuming that the data will not change (or at least the hash value for the record will not change depending regardless of the data's value) and when the hash value changes, it can't "find" the record to unselect it). By removing the custom Equals and GetHashCode, .NET falls back to a more generic version that identifies the object by its reference and not by its values.

Now, on to try a DataGrid to see if that works for me...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜