开发者

problem binding ListBox on ObservableCollection<T>

I have 开发者_StackOverflowa strange "problem". Could someone explain me why :

If I have in an ObservableCollection, twice (or more time) an item with the same value, then the selections of those values in the ListBox won't work properly ?

In fact, what the ListBox is doing when I click on an item(Even in single item selection) : It selects the first item from the ObservableCollection collection with a matching value. so in the case if multiple items with same value are in the collection, then only the first one will be selected !


Because objects you entered to collection have same references. you should create new instances in each case or override Equal function and write your logic for identifying items. WPF ListBox calls Object.Equal function to identify if the items are same.

Hope this helps


You need to create a new object to hold each object.

I.e.

MyCollection.Add(new MyContainer() { Data = myObject } );

This way the listbox will select the objects properly as it has unique containers.

This would be implicit if you were using ViewModels

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜