DataBinding and multiple Selected Items in ListBox

When working with a ListBox set to Multiple Selection Mode, it is often necessary to do something with the selected items.  An easy way to do this is shown in the code below.  In this quick example using type “Part”, we get the selected items from the list box, and remove them from the List.    By implementing an ObservableCollection<Part> and using databinding in our XAML, the Listbox will be updated automatically as items are deleted.  Be sure to include the using statement:  using System.Collections.ObjectModel

 revSelItemsLB

Set the ListBox DataBinding in your XAML to the name of the collection, in this case “MyParts”

revSelItemsLB2

Edit the Generated Item Container Template in Blend, and bind the “Content” property to “Name”

revSelItemsLB3

So now, when the app is run, the ListBox is populated, and when an item is selected, then deleted with a click of the button, the Listbox is updated automatically.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s