0

Is there a simple solution to remove specific itemS from ListView.SelectedItems?

I've a ListView bound to an ObservableCollection<MyClass> (MyClass has some attributes e.g. Name).

Something like:

mylistview.SelectedItems.Remove(FROM myClassItem IN mylistview.SelectedItems WHERE myClassItem.Name == "test");

Of course, it doesn't work.

user1011394
  • 1,656
  • 6
  • 28
  • 41
  • 1
    Seems to be that you are looking for something like this: http://stackoverflow.com/questions/853526/using-linq-to-remove-objects-within-a-listt#comment14709180_853551 – Smileek Jul 26 '12 at 14:53

1 Answers1

1
foreach (var item in mylistview.SelectedItems
                               .Cast<ListViewItem>()
                               .Where(lvi => lvi.Name == "test")
    item.Remove();
Raphaël Althaus
  • 59,727
  • 6
  • 96
  • 122
  • If I'm not mistaken, this might cause run time exception, as it modify the collection while it iterate over it. – Tamir Jul 26 '12 at 15:10
  • 1
    @Tamir http://stackoverflow.com/questions/8379072/why-foreach-works-while-removing-items-from-listview-and-doesnt-work-from-listb – Raphaël Althaus Jul 26 '12 at 15:15