0
votes

I am using the multi-platform Xamarin forms, and I want to be able to click a button and select all the items inside a collectionview.

I have :

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

and inside the cs file:

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

However that does not work as SelectedItems requires an IList which does not accept normal List or ObservableCollection. I also tried setting the SelectedItems to collectionList.ItemsSource (the collectionview source) but that gave the same type error which is Cannot implicitly convert type 'System.Collections.Generic.List<object>' to 'System.Collections.Generic.IList<object>'

What I have currently working is that the collectionview populates with data, and multiple items can be manually selected by the user. This returns me the items that were tapped. I cannot get the view to select everything programmatically though.

I have searched all over and I haven't seen any posts trying to do this, what is the best approach to making a "select all" button for collectionviews?

Edit: The problem was that my list had a defined class as the list type and it needed to be of type object as listed in the accepted answer below

1

1 Answers

1
votes

I just tried this and it works

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

there are undoubtedly other approaches, but not that I can think of on a Sunday afternoon