Xamarin Forms CollectionView Select All Items - xamarin

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

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

Related

Xamarin Forms - Slide out menu to select items

I have a xamarin forms application search screen that allows a user to select an item from a list. Currently I am using a picklist and the user has to scroll and select and click done. This is using the native controls. Howerver I have noticed in iOS in settings menu etc where the have designed it so that when a user want to select from a list a arrow is shown on the right hand side which takes them to another page to select an item (s) from a list. To me this seems like a better UX. Here is an ex:
My question is there something built into this withing xamarin forms? I guess I could create a listview and then on click pass to another page. Any examples are appreciated.
There is no built-in for that, but it's not too hard to achieve this.
To set the disclosure indicator (the arrow), implement a custom renderer derived from ImageCellRenderer and override GetCell
public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
{
var viewCell = base.GetCell(item, reusableCell, tv);
viewCell.Accessory = UITableViewCellAccessory.DisclosureIndicator;
return viewCell;
}
When the user taps that cell you should navigate modally to the view showing the list of languages.
Adding that Cancel-Button is not really hard either, but needs another step. You'll need to wrap the new language selection page in a NavigationPage and push that NavigationPage modally. Furthermore you'll have to add a toolbar item to the wrapped page (see here).
Within the page there is a SearchBar view and a ListView below it. To add the checkmarks, you'll have to implement a custom cell with a custom renderer (derived from ImageCellRenderer like the one shown above), setting the UITableViewCell.Accessory to Checkmark if the custom cell is selected
if(item is SelectableCell selectableCell)
{
var selected = selectableCell.IsSelected;
viewCell.Accessory = selected ? UITableViewCellAccessory.Checkmark : UITableViewCellAccessory.Checkmark;
}
Please note: Selectable cell is no Xamarin.Forms stock cell, but one that you have to implement by yourself, including the bindable property IsSelected.
This should be basically the steps you need to achieve what you want. I've assumed basic Xamarin.Forms knowledge that is needed to fill the gaps.

Filter ObservableCollection based on checkbox listbox

I can't find any nice solution to solve my problem. All that I think about are good but to long I guess. So here is the thing:
I have map with pushpins binded from ObservableCollection and filter ApplicationBar button on bottow which navigates to set filters page.
Filters page is listbox with checkboxes. Based on which checkboxes are checked I want to filter my pushpins on map.
And btw. my pins data comes from sqlite. So generaly I can do WHERE type = 'name1' OR WHERE type='name2' and it will do it well.
The only thing I am missing here is how to fill this query, I mean how to get values from listbox and smoothly send them to my map page.
I hope that wasn't too chaotic :)
You should have a look at the ICollectionView interface : http://msdn.microsoft.com/en-us/library/ms752348.aspx

WP7 Listbox - How to ensure selected item is visible via Binding

Using MVVM Light I have a listbox databound to a collection of several items.
One of the items is the selected one.
With this code it works perfectly and ViewModel structures are updated correctly:
<Name="listBox1"
ItemsSource="{Binding Path=Models}"
SelectedItem="{Binding Path=csProfile.Model, Mode=TwoWay}">
My problem is that when I enter the page if the selected Item is not in the first items it is not visible and the user does not know what was the previous selection.
How could I force the Listbox to always show the Selected Item?
Possibly via properties or Binding.
M
There's no property you can bind to to set what's visible. Instead call ScrollToVerticalOffset() on the ScrollViewer inside the ListBox.
I think you are looking for the ScrollIntoView method. A similar topic was discussed here:
Automatic Scrolling in a Silverlight List Box
The API reference is here: http://msdn.microsoft.com/en-us/library/system.windows.controls.listbox.scrollintoview(VS.95).aspx
Calling UpdateLayout() before calling ScrollIntoView on the selected item seems to be necessary.

Windows Phone 7 - passing selected item (listbox databound to ObservableCollection) to "update" page

I am writing a Windows Phone 7 Application that should be pretty basic.
I have a view model ("MainViewModel") that contains a class I created ("EntrySheet"), which contains an ObservableCollection (I'll refer to it as "Entries").
My MainPage.xaml contains a listbox that is databound to the App.ViewModel.EntrySheet.Entries. This works perfectly for showing the list, and adding entries to the ObservableCollection is reflected in the ListBox.
However, what I can't get my head around is the proper way to "pass" a selected Entry to an update page (this will let a user change fields on the Entry: name, amount, date, whatever which when navigated back to MainPage will be reflected in the ListBox).
I guess what I expected what for there to be a "SelectedItem" on the ObservableCollection, and I could just navigate to the update page, which would be able to use something like: App.ViewModel.EntrySheet.Entries.SelectedItem.
I really appreciate any help on this and would also welcome constructive criticism on how to better structure my app.
I know this is a late anwer, but I just happened to stumble across your question now.
Here's a possible solution for you:
XAML
<ListBox Name="listBox" Tap="listBox_Tap">
<!-- Some code -->
</ListBox>
C#
private void listBox_Tap(object sender, GestureEventArgs e)
{
DataObject dataObject = (sender as ListBox).SelectedItem as DataObject;
//Do something with dataObject
}
This will enable you to interact with the data object behind the selected item in the listbox.
Courtesy of Windows Phone Geek

How to bind test data to a SketchFlow ComboBox?

I haven't been able to successfully bind some test data to a SketchFlow ComboBox. I added a sample data source and created a collection with some basic string values. There are a number of tutorials on how to bind to a ListBox in SketchFlow and that works fine, just not finding anything for a ComboBox.
One thing that caught me out a few times is that you need to have your data in "list mode" before you drag it on to the control (combo box or list box).
It seems to work as I expected, so perhaps I'm not understanding your issue correctly, but I'll give it a shot! I created a sample DataSource with a couple of strings in it, added a ComboBox to the layout root and then just dragged the Collection from the DataSource and dropped it on the ComboBox.
Keep in mind that if you drag the DataSource itself, you'll only be setting the DataContext property of the ComboBox and that's not enough to get your items to display. You also need the ItemsSource bound to the Collection of the DataSource and an ItemTemplate or a DisplayMemberPath to tell the ComboBox how to display your items.
Additionally, if you use the drag and drop method of binding, it will use the ItemTemplate approach for you, which may or may not be what you want as it will generally create a StackPanel and display all fields from the row in your Collection per item in the ComboBox. You can remove the ItemTemplate and set DisplayMemberPath to whichever field you wish to have displayed from your data source's collection.

Resources