views:

232

answers:

1

I have a listbox bound to a view model observable collection:

This works fine, minus one little hitch... assuming that the observable collection contains strings, the whole thing breaks down when entries with identical values get added to the collection, what is the best way to handle this? Custom struct instead of strings and then a datatemplate?

edit: completely forgot to explain the behavior... it selects multiple values when i click on a single entry, all other values with the same text get selected.

+2  A: 

This problem exists because its the nature of the Selector derived control. When you start to select an item from the ObservableCollection type and your collection contains duplicate strings, the selector is confused as to which item it has selected. You need to create a new simple class/struct and put your string in there.

public class Info
{
    public string Name { get; set; }
}

// ..

MyList = new ObservableCollection<Info>(new List<Info> { new Info { Name = "Hello World" }, new Info { Name = "Hello World" }, new Info { Name = "Hello World" } });

and like so.

<ListBox ItemsSource="{Binding MyList}" DisplayMemberPath="Name" />
Tri Q
this isn't exactly what I did, I used a more detailed structure and a data template, but that works ^_^
Firoso