简体   繁体   English

Xamarin 表单 CollectionView 预选不起作用

[英]Xamarin forms CollectionView preselected not working

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):这可以工作,第一个元素是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.我试过覆盖对象等于函数和运算符 ==,两者都不起作用。

I think it's because you didn't assign the data to the same reference object.我认为这是因为您没有将数据分配给同一个引用对象。

_categoryRepository.Get(); and _categoryRepository.Get()[0];_categoryRepository.Get()[0]; maybe two different references.也许是两个不同的参考。

When you set CategoryCollection.ItemsSource = _categoryRepository.Get();当您设置CategoryCollection.ItemsSource = _categoryRepository.Get(); ,it's like ,就像是

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

when you CategoryCollection.SelectedItem = _categoryRepository.Get()[0];当你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.但是 list1 和 list2 可能不是同一个引用对象。所以它无法识别选择项。

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. temp 始终是唯一的,因此它可以识别选择项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM