Detect if an item is selected or not in the list (WPF)

I want to know how you can find out if an item is selected or not in the items array from the list. There are several options in the list, so I need to iterate all of them and see which ones are selected and which are not.

Thanks a lot (I know - short and sweet)

+4
source share
3 answers

Take a look at the SelectedItems property and repeat this to see which items are selected.

If you want to view all the elements, you can compare the two collections (MyListBox.Items and MyListBox.SelectedItems) and see which ones correspond.

sort of:

foreach(Item item in MyListBox.Items) if(MyListBox.SelectedItems.Contains(item) MyObject.Value = true; else MyObject.Value = false; 

Overkil, though really! I suppose there is a goal, if you want to do something to all the elements that are not selected, is that what you want to do?

There are much better ways to do this, although Randolpho is the right one, data binding will be the best way to do this, depending on how your data is organized / entered and how big the list is.

+4
source

ListBox has the SelectedItems property. This collection will contain links to selected items.

+1
source

I have not worked on WPF and it is purely based on MSDN.
See SelectedItems propeerty.

+1
source

Source: https://habr.com/ru/post/1313193/


All Articles