Bug in WPF ListView (Single Selection Mode)

by sondlerd
AddThis Social Bookmark Button

26. August 2007 12:04

I have found that there are some strange happenings with the ListView in WPF (as of 8/2007).  There seem to be some internal bugs dealing with internal selection properties.  I have the ListView selection mode set to single (SelectionMode="Single") and I although I cannot select more than one item using control or shift, when I return from an edit the ListView selects multiple items.

See how the SelectedItems has a count of 2 and SelectionMode is single:

 

 When the code returns to the application, it then proceeds to select multiple items:

This is a strange issue, obviously a ListView bug.

 Here is my work around, found by colleage Tom Sprows.  Simply set the selected item to multiple, clear the selected items, do the operation and set the selection mode back to single.

When the row is selected and a method that will perform some operation upon the selected item.  You must do the following:

1. Get the selected item from the ListView and set it to a local variable.
Product selectedProduct = listView.SelectedItem as Product;

2.  Set the Selection Mode to multiple
listView.SelectionMode = SelectionMode.Multiple;

3. Clear the selectedItems using the object.SelectedItems.Clear()
listView.SelectedItems.Clear();

4. Perform whatever operation you are going to perform on the selected item and when it returns, set the Datalist.SelectedItem back to the originally selected item.
PerformOperation(selectedProduct);
listView.SelectedItem = selectedProduct;

5. Set the Selection mode back to single
listView.SelectionMode = SelectionMode.Single;

 

 

Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Related posts

Comments

December 13. 2007 11:04

Gravatar

Any Idea if there is no other workaround on this issue?
I am surprized that this well known bug was not fixed in the release of .Net 3.5

Pierre us

December 13. 2007 11:07

Gravatar

I noticed in 3.5, it visually happens only when you scroll the listview at that point all the items that were once selected reselect themselves.

Pierre us

January 31. 2008 19:44

Gravatar

I dont' know another work around. We found this one and moved on. Ya, I agree, I am very surprised that this got out the door as Microsoft... it is a pretty big bug.

I noticed that the scrolling did has some effect on the selection as well... it's like it repaints the selected items after you scroll.

thanks for your imput.

sondlerd us

July 18. 2008 16:03

Gravatar

I am just curious. I have tried to use

Customer cust =ListViewUserDetails.SelectedValue as Customer;

in the similar lines as you have used for product to get the selected product details. I get null in the cust values. Am I doing something wrong?
The intention is just to get the one of the columns values of the selected item.

Naveen us

July 26. 2008 11:44

Gravatar

Are you sure there is data there to begin with? How did you bind your control?

Rob Sondles us

Add comment


(Will show your Gravatar icon)  

  Country flag




Live preview

November 21. 2008 22:36

Gravatar

Powered by BlogEngine.NET 1.1.0.7
Theme by Mads Kristensen

Robert D. Sondles

Name of author Robert Sondles
is an independent software engineer operating under his company named Blueberry Island Media ltd. He started the company in 2003 and has been developing softare with .Net since 2002. He is located in Philadelphia, PA..

E-mail me Send mail

Calendar

<<  November 2008  >>
MoTuWeThFrSaSu
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

View posts in large calendar

Recent comments

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Sign in