Create Multiselect Listview with Xamarin Forms(Xamarin Cross Platform)

本小妞迷上赌 提交于 2019-12-23 03:40:44

问题


I am trying to implement a Listview with Checkbox controll in each item of Listview.If I want to delete two items I will check those two items and click of delete it should delete.I explored more about this kind of concept but not able to find single demo example in Github as well.

Programming Language:Xamarin forms not in Xamarin android or Xamain IOS.I already know how to implement in those platforms.But I dont have any sample code at least to for better understanding of "Multiselect and deletion operation in Xamarin Forms"


回答1:


  • Use SwitchCell (example here: https://github.com/xamarin/xamarin-forms-samples/tree/master/UserInterface/ListView/SwitchEntryTwoBinding/twoWayBinding))

or create custom ViewCell layout (https://developer.xamarin.com/guides/cross-platform/xamarin-forms/user-interface/listview/customizing-cell-appearance/ - Custom Cells chapter)

  • Bind SwitchCell.OnProperty or YourCustomViewCell.Checkbox.CheckedProperty with your ViewModel.ObservableCollection.IsChecked property (as in SwitchEntryTwoBinding example)

  • Then you could make a Button or ToolbarItem which calls method that iterates every item in ViewModel.ObservableCollection and deletes it if IsChecked=true.




回答2:


First create a custom cell with a switch (or implement SwitchCell). Then bind the switch's value to a bool in your data model. Then, on the button clicked event, a simple Linq query should do the trick.

Make sure your list of items is an ObservableCollection, so when you delete items, changes are correctly propagated to the ListView.




回答3:


We have created a Multi Select ListView control here. It works on all platforms and has no platform specific code. https://github.com/MelbourneDeveloper/Adapt.Presentation/blob/master/Adapt.Presentation.Standard/Adapt/Presentation/Controls/AdaptListView.cs

There is a full sample included here: https://github.com/MelbourneDeveloper/Adapt.Presentation.git

This works very well with async behaviour. I.e. you can set the ItemsSource, or the SelectedItems in any order. There is a multi select sample in the repo: https://github.com/MelbourneDeveloper/Adapt.Presentation.git

There is also a behavior to toggle the selection mode with a button so that you can either jump off to a selected record, or select many records in the listview: https://github.com/MelbourneDeveloper/Adapt.Presentation/blob/master/Adapt.Presentation.Standard/Adapt/Presentation/Behaviours/AdaptListViewSelectionModeToggleBehavior.cs

This behavior is related because it will allow you to delete selected items from a listview: https://github.com/MelbourneDeveloper/Adapt.Presentation/blob/master/Adapt.Presentation.Standard/Adapt/Presentation/Behaviours/RemoveFromCollectionBehavior.cs

Unfortunately, this doesn't currently support CheckBoxes, but the functionality works without the need for CheckBoxes, and we may add that feature in future.




回答4:


MVVM approach of how to have a Xamarin.Forms.ListView that holds a set of items that can be selected for deletion. Deletion is initiated through a button command.

View:

  • Holds ListView
  • ListView Cells are either SwitchCell or custom cell with a Control that can bind to a boolean (preferably a Xamarin.Forms.Switch)
  • Holds a Button "Delete Items"

Model:

  • Implements INotifyPropertyChanged interface (to update view)
  • Holds whatever additional data
  • Holds a public bool ShouldBeDeleted { ... setter calls PropertyChanged() ...), this will be bound to the Xamarin.Forms.Switch.IsToggled bindable property

ViewModel:

  • is the BindingContext of the View
  • Holds a IList/IEnumerable of instances of the Model
  • The IList/IEnumerable will be set to the "ItemsSource"-Property of the Views ListView
  • Holds a command that binds to the Command property of the "Delete Items"-Button. This command should call a method that loops through the IList/IEnumerable and removes any item that has ShouldBeDeleted as true.
  • use ObservableCollection for the ItemList. it updates the View when models are added, removed, or the list is cleared/refreshed


来源:https://stackoverflow.com/questions/32136815/create-multiselect-listview-with-xamarin-formsxamarin-cross-platform

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!