问题
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
orYourCustomViewCell.Checkbox.CheckedProperty
with yourViewModel.ObservableCollection.IsChecked
property (as in SwitchEntryTwoBinding example)Then you could make a
Button
orToolbarItem
which calls method that iterates every item inViewModel.ObservableCollection
and deletes it ifIsChecked=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 eitherSwitchCell
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 theXamarin.Forms.Switch.IsToggled bindable property
ViewModel:
- is the
BindingContext
of the View - Holds a
IList/IEnumerable
of instances of theModel
- 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