WPF ListView / GridView single selection error

ListView / GridView in WPF gets weird error when SelectionMode is set to Single.

Play methods:

Create a collection (the collection must have enough items for the ListView to scroll at least 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 to 3-4 positions. Remember which items you previously selected. Scroll down with the mouse wheel to go to the next page. Scroll through the backup. Voila !! Will you see all the items you selected? Image of this error: http://img261.imageshack.us/img261/133/listview.jpg

This same issue harms the Wpf toolkit datagrid.

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

Can someone tell me why this is happening? Is it planned or a mistake? It seems to be more of a bug.

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

+2


source to share


3 answers


This is interesting and may be a mistake. This is such a general control and scenario, although I suspect something else might be going on. I found a link to something similar to this problem here . The advice he decides follows:

If you are overriding Equals in a ListView displayable, do it right. Otherwise, you end up with all sorts of interesting behavior ...



I guess the theory here is that somehow the error in Equals will throw away logic in the ListView. Make sure you don't override Equals and post your results. If you don’t, and you don’t get any other help here, I recommend that you file this as a bug on Microsoft Connect .

+8


source


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

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

My ListView had an ItemSource associated with the ObservableCollection.



I found this behavior was only when I had 5000 items in my collection. I fixed the problem by creating a temporary collection and then setting the linked collection. This reduced the updates in my ListView to one update.

This may not be the most correct solution, but it works in my case and hopefully helps someone else.

0


source


Throwing my thoughts into this old thread ... I had this problem too, and yes by removing my own Equals and GetHashCode values ​​they fixed it, but unlike the quote in Jerry's answer, my Equals and GetHashCode override methods did indeed spelled correctly.

For me the problem manifested itself when the selected data was changed. Even though the changes were displayed correctly in the ListView, it remained selected after that.

While I can't find a good explanation for this, my personal guess is that the code in the ListView might assume that the data won't change (or at least the hash value for the record won't change depending on data value), and when the hash value changes, it cannot "find" the entry to deselect). By removing the custom Equals and GetHashCode, .NET falls back to a more general version, which identifies an object by its reference rather than its values.

Now to try the DataGrid to see if it works for me ...

0


source







All Articles