Answered by:
How to clear the selection from a ListView

Question
-
Hi
I'm getting an exception when trying to clear the selection from a ListView.
For example:
Start a new Windows Store app in C#, choose blank.xaml
Add a ListView and a Button to MainPage.xaml. Name the ListView "Test".
Add some items to the ListView eg:
<ListViewItem Content="One"/> <ListViewItem Content="Two"/> <ListViewItem Content="Three"/>
Add some code to the click handler for the button eg:
Test.SelectedItems.Clear();
At runtime, I get a "Catastrophic failure" exception when clicking the button, whether or not anything is selected in the ListView.
I have found a workaround:
Test.SelectionMode = ListViewSelectionMode.None; Test.SelectionMode = ListViewSelectionMode.Single;
which clears the selection successfully. But that can't be the right way to clear the selection?Tim
Monday, April 21, 2014 1:02 PM
Answers
-
Hi Tim,
Good question, I use following code to unselect the ListView
Test.SelectedIndex = -1;
Hope helps.
--James
<THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
Thanks
MSDN Community Support
Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.- Proposed as answer by Ricky_Brundritt Wednesday, April 23, 2014 1:21 PM
- Marked as answer by Tim Anderson Wednesday, April 23, 2014 1:24 PM
Tuesday, April 22, 2014 1:52 AMModerator -
For a single selection ListView set SelectedItem to null. For a multiple or extended selection ListView you can also call Clear() on the SelectedItems collection.
You will get an exception if you call SelectedItems.Clear on a SelectionMode.Single ListView.
--Rob
- Proposed as answer by Ricky_Brundritt Wednesday, April 23, 2014 1:22 PM
- Marked as answer by Tim Anderson Wednesday, April 23, 2014 1:24 PM
Wednesday, April 23, 2014 12:18 AMModerator
All replies
-
please see if UnselectAll works:
Test.UnselectAll();
hope this helps!
Monday, April 21, 2014 1:19 PM -
Hi Shreeharsh
No such method apparently, for Store apps.
Tim
Monday, April 21, 2014 1:37 PM -
Hi Tim,
Good question, I use following code to unselect the ListView
Test.SelectedIndex = -1;
Hope helps.
--James
<THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
Thanks
MSDN Community Support
Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.- Proposed as answer by Ricky_Brundritt Wednesday, April 23, 2014 1:21 PM
- Marked as answer by Tim Anderson Wednesday, April 23, 2014 1:24 PM
Tuesday, April 22, 2014 1:52 AMModerator -
Ah, that works. Thanks.
Tim
Tuesday, April 22, 2014 8:48 AM -
For a single selection ListView set SelectedItem to null. For a multiple or extended selection ListView you can also call Clear() on the SelectedItems collection.
You will get an exception if you call SelectedItems.Clear on a SelectionMode.Single ListView.
--Rob
- Proposed as answer by Ricky_Brundritt Wednesday, April 23, 2014 1:22 PM
- Marked as answer by Tim Anderson Wednesday, April 23, 2014 1:24 PM
Wednesday, April 23, 2014 12:18 AMModerator