public MainPage() { InitializeComponent(); //Set binding context vm = new MainPageViewModel(this); BindingContext = vm; // Hook up event handlers PlanetListView.SelectionMode = ListViewSelectionMode.Single; //Default PlanetListView.ItemTapped += PlanetListView_ItemTapped; //User tapped PlanetListView.ItemSelected += PlanetListView_ItemSelected; //Selection changed // ***************************************************************** // DATA TEMPLATE // ***************************************************************** //Data template will instantiate a cell "when required" //Two options: I like the second but the first is more common //DataTemplate DataTemplate = new DataTemplate(typeof(TextCell)); DataTemplate dataTemplate = new DataTemplate(() => { //Return a subclass of Cell TextCell cell = new TextCell(); //We can set properties on cell MenuItem m1 = new MenuItem { Text = "Delete", IsDestructive = true }; //Commanding - command is bound directly to the ICommand DeleteCommand = DeleteCommand<SolPlanet> property in ViewModel //This presumably uses reflection to determine the parameter type m1.SetBinding(MenuItem.CommandProperty, new Binding("DeleteCommand", source: this.BindingContext)); // See https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/listview/interactivity#context-actions // The BindingContext for m1 is the data behind the cell (a specific element in the itemsource of the listview) // See last para on p559 of the book by Charles Petzold m1.SetBinding(MenuItem.CommandParameterProperty, new Binding(".")); // Or you could use an event handler /* * m1.Clicked += (object sender, System.EventArgs e) => { * //Note that conditional statements evaluate left to right - && will short-circuit if the first condition is not true * if ((sender is MenuItem mi) && (mi.CommandParameter is SolPlanet p)) * { * Debug.WriteLine($"Vogons are destroying {p.Name}"); * vm.DeleteItem(p); * } * }; */ //Add menu item to the cell cell.ContextActions.Add(m1); return(cell); }); //Binding proxy: When the DataTemplate instantiates a cell, it will also set up the binding as specified below //The source will be a data elelement dataTemplate.SetBinding(TextCell.TextProperty, "Name"); dataTemplate.SetBinding(TextCell.DetailProperty, "Distance"); //Finally, set the ItemTemplate property (type DataTemplate) PlanetListView.ItemTemplate = dataTemplate; // ***************************************************************** }