This is because you add the same object to the list twice. The ListBox control cannot determine between. One way to solve this problem is to wrap each element in a different class:
lbItems.Items.Add(new WrappedThing((a)); lbItems.Items.Add(new WrappedThing((b)); lbItems.Items.Add(new WrappedThing((a)); lbItems.Items.Add(new WrappedThing((b));
... This means that each element in the list is unique, although the element that they wrap may not be. Note that any data template or binding would also need to be changed to support this, although you could do it with one global DataTemplate .
WrappedThing will look something like this:
class WrappedThing<T> { public WrappedThing(T thing) { Thing = thing; } public T Thing { get; private set; } }
(Note: this is copied from my answer to another question here , as the answer is useful, but the question is slightly different.)
Dan puzey
source share