Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing an item from a WPF binding listbox

I have a WPF application with a ListBox (called listMyItems) which is successfully bound to a class of MyItems that I created. I have a List of MyItems called currentMyItems which is then assigned as ItemSource to the ListBox. It all works fine, if I add an item to the currentMyItems it pops up on the list, etc. The problem occurs when I try to remove the selected item in the ListBox. This is the code that I use:

currentMyItems.Remove((MyItem)listMyItems.SelectedItem);

The item disappears from the ListBox but the next time I update it, it pops back up as it was never deleted. Any tips?

like image 395
mmvsbg Avatar asked Dec 02 '22 17:12

mmvsbg


2 Answers

I think you may be confused about how data binding works. When you bind a property, you are telling WPF to go look somewhere else for the value of that property.

When you bind the ListBox.ItemsSource property to currentMyItems, you are telling WPF to go look at the currentMyItems list to find its list of items. If currentMyItems is an ObservableCollection instead of a List<T>, then the UI will automatically receive a notification to update the bound value when you add or remove an item from the collection.

Based on what you say in the question, it sounds like you you have two collections, one of which is bound, and the other which is used to recreate the first collection anytime a change occurs. All that is not needed.

Just create one ObservableCollection<MyItem>, bind it to the ListBox.ItemsSource property, and then add or remove items from that single collection. It should work as you would expect.

<ListBox x:Name="listMyItems" ItemsSource="{Binding MyItems}" />

and

MyItems.Add((MyItem)listMyItems.SelectedItem)
MyItems.Remove((MyItem)listMyItems.SelectedItem)

If you're interested, I also have some beginner articles on my blog for WPF users who are struggling to understand the DataContext. You may want to check out Understanding the change in mindset when switching from WinForms to WPF and What is this “DataContext” you speak of?

like image 91
Rachel Avatar answered Dec 04 '22 07:12

Rachel


If you bound it correctly to an ObservableCollection and currentMyItems is that collection. Than it means that you must have reloaded currentMyItems in meantime.

Also consider binding the SelectedItem property of your ListView - your view model doesn't have to know about the view at all.

like image 33
Andrzej Gis Avatar answered Dec 04 '22 05:12

Andrzej Gis