When initializing the form, set the List Box’s Display Member and Value Member properties to your classes properties e.g. Add(new List Item(){ Text = “some text”, Value = “some value”}); Step 4. in the Listbox’s Selected Index Changed event read the value by casting the Listbox’s selected item as your custom class and then reading the property you need.

databound listbox not updating-80databound listbox not updating-86

I was faced with a bit of a challenge this morning.

In Web Forms getting the value of the selected item in a List Box is pretty straight forward. Typically this is just the Text that is going to be displayed.

However what i found in Web Forms as a different story. However adding an item where the Text shows one thing, but the value is a different thing requires a slightly different approach Step1.

To complicate the process I was adding items to the listbox using the items collections add method. Add a new Class to your project that has a Text and Value property. private class List Item { public string Text { get; set; } public string Value { get; set; } } Step 2.

As no one gave me an answer, I was forced to use my gray-matter and invent a solution of my own.

Since a similar question came up again recently on Stack Overflow, I thought I’d share what I came up with.

In my solution, I define an attached property that you attach to a List Box (or Data Grid, or anything that inherits from Multi Selector) and allows you to specify a collection (via data binding, of course) that you want to be kept in sync with the Selected Items collection of the target.

To work properly, the collection you give should implement INotify Collection Changed – using Observable Collection should do the trick.

When you set the property, I instantiate another class of my invention, a Two List Synchronizer.