We can add child controls to Listview in Xamarin.Forms. In order to see the child control in action we will take an example. Let’s display a list of data in the Listview and perform delete option on the list data per row basis.
Since we need to add multiple columns in the list view, we need to use the DataTemplate to define the custom view. Let’s use a model class to assign the data to the custom view.
- public class Item
- {
- public string ItemName { get; set; }
- public string ItemDetails { get; set; }
- }
Time to add event to the child control
- <Button Text="Delete" CommandParameter="{Binding ItemName}" Clicked="DeleteClicked"></Button>
How the final XAML look like then ?
- <?xml version="1.0" encoding="utf-8" ?>
- <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
- x:Class="RelativeLayout.CustomerForm">
- <StackLayout>
- <Label x:Name="lbl1" Text="Items" VerticalOptions="StartAndExpand" HorizontalOptions="Center"></Label>
- <ListView x:Name="lstItems" VerticalOptions="FillAndExpand">
- <ListView.ItemTemplate>
- <DataTemplate>
- <ViewCell>
- <ViewCell.View>
- <StackLayout Orientation="Horizontal">
- <Label Text="{Binding ItemName}" HorizontalOptions="StartAndExpand" FontSize="30"></Label>
- <Button Text="Delete" CommandParameter="{Binding ItemName}" Clicked="DeleteClicked">
- </Button>
- </StackLayout>
- </ViewCell.View>
- </ViewCell>
- </DataTemplate>
- </ListView.ItemTemplate>
- </ListView>
- </StackLayout>
- </ContentPage>
- public partial class CustomerForm : ContentPage
- {
- public List<Item> allItems;
- public CustomerForm()
- {
- allItems = new List<Item>
- {
- new Item(){ItemName = "Rice", ItemDetails = "Rich in carbohydrates"},
- new Item(){ItemName = "Dal", ItemDetails = "Rich in protine"},
- new Item(){ItemName = "Milk", ItemDetails = "Rich in protine"},
- };
- InitializeComponent();
- lstItems.ItemsSource = allItems;
- }
- public void DeleteClicked(object sender, EventArgs e)
- {
- var item = (Xamarin.Forms.Button)sender;
- Item listitem = (from itm in allItems where itm.ItemName == item.CommandParameter.ToString() select
- itm).FirstOrDefault<Item>();
- allItems.Remove(listitem);
- }
- }
Running fine. OOOPSSSSSSS… but the deletion is not working. That’s correct. Because, the item is getting deleted from the collection but the ItemSource is not getting refreshed.
What we need to do then ?????
Pretty simple. Let’s use an ObservableCollection to assign theItemSource.
Modifying the Model Class
- public class Item
- {
- public string ItemName { get; set; }
- public string ItemDetails { get; set; }
- }
- public class ItemList : INotifyPropertyChanged
- {
- public event PropertyChangedEventHandler PropertyChanged;
- public ObservableCollection<Item> _items;
- public ObservableCollection<Item> Items
- {
- get { return _items; }
- set { _items = value; OnPropertyChanged("Items"); }
- }
- protected virtual void OnPropertyChanged(string propertyName)
- {
- if (PropertyChanged == null)
- return;
- PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
- }
- public ItemList(List<Item> itemList)
- {
- Items = new ObservableCollection<Item>();
- foreach (Item itm in itemList)
- {
- Items.Add(itm);
- }
- }
- }
Small changes in the code behind
- public partial class CustomerForm : ContentPage
- {
- public List<Item> allItems;
- ItemList items;
- public CustomerForm()
- {
- allItems = new List<Item>
- {
- new Item(){ItemName = "Rice", ItemDetails = "Rich in carbohydrates"},
- new Item(){ItemName = "Dal", ItemDetails = "Rich in protine"},
- new Item(){ItemName = "Milk", ItemDetails = "Rich in protine"},
- };
- InitializeComponent();
- items = new ItemList(allItems);
- lstItems.ItemsSource = items.Items;
- }
- public void DeleteClicked(object sender, EventArgs e)
- {
- var item = (Xamarin.Forms.Button)sender;
- Item listitem = (from itm in items.Items where itm.ItemName == item.CommandParameter.ToString() select itm).FirstOrDefault<Item>();
- items.Items.Remove(listitem);
- }
- }
How is the output ?
Happy coding



Jose PicadoPosted Aug 30, 2018, 3:17 PM
Excelente te agradezco mucho, busque por todo lado y no habia encontralo la ayuda que me diste
MANOJ JENAPosted Dec 15, 2017, 11:36 AM
Hi Nirmal, I have a requirement where I want to create 100 or more switch/toggle buttons and want to capture each Switch Button toggle action . Is there any simple way to create one function to capture all the ToggleEvents for Elements and publish the state when the application is loaded??
Luis MunozPosted Dec 30, 2016, 3:29 PM
Hi, this is so nice, you really save my day, but i have a question, this doesn't work in windows phone, is there any posible solution? thanks
Prajakta ShindePosted Aug 8, 2016, 5:47 AM
Hi, I have picker in each row of listview. How to get value of picker and how to get from which row it is selected? Xamarin.Forms
BUISSART THOMASPosted Jun 29, 2016, 10:14 AM
Here, this design should not be followed, because it is a kind of hack of event pattern, using a CommandParameter (from ICommand Api). You implement an ICommand, for a certain type of parameter, and then in the xaml, you bind your Button.Command dependency property to your command instanciated in your viewmodel, and bind also the item (Current Binding Context of your ListViewItem) to the Command Parameter, and the job is done, more performant, mvvm compliant.
BUISSART THOMASPosted Jun 29, 2016, 10:11 AM
In mvvm, you would bind your button to a Command.
sivashankar arumugamPosted Jun 20, 2016, 8:41 AM
Can you please share the workaround using mvvm in xamarin forms
Karthik Muthu KaruppanPosted Apr 16, 2015, 11:32 AM
Good one
Gowtham RajamanickamPosted Apr 16, 2015, 8:40 AM
keep writing
Gowtham RajamanickamPosted Apr 16, 2015, 8:40 AM
good one