Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/syncfusionexamples/xamarin-forms-listview-selected-item-binding-selectionchanging-event

This repository contains the sample about how to bind selectionchanging event in Xamarin.Forms ListView
https://github.com/syncfusionexamples/xamarin-forms-listview-selected-item-binding-selectionchanging-event

listview mvvm selected-item selection selectionchange xamarin xamarin-forms

Last synced: about 1 month ago
JSON representation

This repository contains the sample about how to bind selectionchanging event in Xamarin.Forms ListView

Awesome Lists containing this project

README

        

# Binding SelectionChanging event to Listview

In ListView, the [SelectionChanging](https://help.syncfusion.com/cr/cref_files/xamarin/Syncfusion.SfListView.XForms~Syncfusion.ListView.XForms.SfListView~SelectionChanging_EV.html) event will be raised when selecting an item at the execution time. MVVM for the `SelectionChanging` event can be achieved by binding through the event to command converter.

Refer [EventToCommand](https://www.syncfusion.com/kb/7523/how-to-turn-events-into-commands-with-behaviors-in-sflistview?_ga=2.49538854.1823797169.1563166718-1085055173.1562420655) knowledge base to create the command for event using behavior.

```



```
```
//ViewModel.cs
public class BookInfoRepository : INotifyPropertyChanged
{
private Command selectedItem

public Command SelectedItem
{
get { return this.selectedItem; }
set
{
this.selectedItem = value;
this.OnPropertyChanged("SelectedItem");
}
}

public BookInfoRepository()
{
selectedItem = new Command(OnSelectionChanging);
}

///
///To disable the selection for particular item
///
public void OnSelectionChanging(ItemSelectionChangingEventArgs obj)
{
var eventArgs = obj as ItemSelectionChangingEventArgs;
if (eventArgs.AddedItems.Count > 0 && eventArgs.AddedItems[0] == this.BookInfoCollection[0])
eventArgs.Cancel = true;
}
}
```
To know more about MVVM in ListView, please refer our documentation [here](https://help.syncfusion.com/xamarin/sflistview/mvvm)