【问题标题】:Xamarin Forms CollectionView Select All ItemsXamarin Forms CollectionView 选择所有项目
【发布时间】:2020-10-18 22:59:45
【问题描述】:

我正在使用多平台 Xamarin 表单,我希望能够单击一个按钮并选择 collectionview 中的所有项目。

我有:

<CollectionView x:Name="collectionList" SelectionMode="Multiple"
                            SelectionChanged="RowSelected"
                            ItemsSource="{Binding ContentList}">

在cs文件里面:

ObservableCollection ContentList = //list made in an earlier method, this is confirmed working
collectionList.SelectedItems= ContentList; //executes on the button click event

但是,这不起作用,因为 SelectedItems 需要一个不接受普通 ListObservableCollectionIList。我还尝试将 SelectedItems 设置为 collectionList.ItemsSource (collectionview 源),但这给出了相同的类型错误,即Cannot implicitly convert type 'System.Collections.Generic.List&lt;object&gt;' to 'System.Collections.Generic.IList&lt;object&gt;'

我目前的工作是collectionview 填充数据,并且用户可以手动选择多个项目。这会返回我被点击的项目。我无法让视图以编程方式选择所有内容。

我已经搜索了所有内容,但没有看到任何尝试这样做的帖子,为收藏视图制作“全选”按钮的最佳方法是什么?

编辑:问题是我的列表有一个定义的类作为列表类型,它需要是object 类型,如下面接受的答案中所列

【问题讨论】:

    标签: xamarin xamarin.forms observablecollection collectionview


    【解决方案1】:

    我刚试过,效果很好

    collectionList.SelectedItems = new ObservableCollection<object>(ContentList);
    

    毫无疑问还有其他方法,但不是我在周日下午能想到的

    【讨论】:

    • 这也不起作用,因为添加 ToList() 运算符会将数据转换为 List 类型,正如原始问题中提到的那样, 不支持IList 并会给出一个隐式转换错误
    • List 继承自 IList。您遇到的具体错误是什么?
    • "无法将类型'System.Collections.Generic.List'隐式转换为'System.Collections.Generic.IList'是错误跨度>
    • 它似乎没有编译,因为我将它作为自定义类型而不是 object 。感谢您的帮助。
    猜你喜欢
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多