开发者

Problems using ListBox and observable collection as a debug log

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 th开发者_StackOverflowe 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.


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" />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜