0
votes

As title, I am using collection view and wanna make default select but it don't show properly.

below is my partial code:

this doesn't work, the first element didn't highlight

CategoryCollection.ItemsSource = _categoryRepository.Get(); // list of object
CategoryCollection.SelectedItem = _categoryRepository.Get()[0];

this can work, the first element was highlight(selected):

var temp = _categoryRepository.Get();
CategoryCollection.ItemsSource = temp ;
CategoryCollection.SelectedItem = temp[0];

I have tried override object equal function and operator ==, both don't work.

1
why are you setting ItemsSource twice? That won't do anything useful. Use the SelectedItem property to pre-select an itemJason
Hi @Jason, I paste the wrong code, it should be 'SelectedItem'user2986600
Could it work ?Leo Zhu - MSFT
Hi @LeoZhu-MSFT, above code would not work.user2986600
Are there any new updates?Leo Zhu - MSFT

1 Answers

1
votes

I think it's because you didn't assign the data to the same reference object.

_categoryRepository.Get(); and _categoryRepository.Get()[0]; maybe two different references.

When you set CategoryCollection.ItemsSource = _categoryRepository.Get(); ,it's like

var list1 = _categoryRepository.Get();
CategoryCollection.ItemsSource = list1;

when you CategoryCollection.SelectedItem = _categoryRepository.Get()[0];,it's like

var list2 = _categoryRepository.Get();
CategoryCollection.SelectedItem = list2[0];

But list1 and list2 may not be the same reference object.So it couldn't recognize the selectitem.

However, when you use

var temp = _categoryRepository.Get();
CategoryCollection.ItemsSource = temp ;
CategoryCollection.SelectedItem = temp[0];

temp is always unique,so it could recognize the selectitem.