Skip to content

Instantly share code, notes, and snippets.

Last active Mar 30, 2020
What would you like to do?
Page with ListView with differend data binding options
<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="" xmlns:x="" xmlns:d="" xmlns:mc="" mc:Ignorable="d" x:Class="DataBindingScopeSample.MainPage" xmlns:local="clr-namespace:DataBindingScopeSample;assembly=DataBindingScopeSample" Title="Nine-Nine">
<!-- Scope is MainPageViewModel, therefore we can set the ItemsSource to its MyItems property -->
<ListView x:Name="ListOfPeople" ItemsSource="{Binding MyItems}" RowHeight="100">
<!-- Here the scope is the ListModel object. That is why we can reference the Title and Image properties -->
<ImageCell Text="{Binding Title}" ImageSource="{Binding Image}">
<!-- You would expect the binding to HighFiveCommand to just work _but_ the command is a property of
MainPageViewModel and not ListModel. And withing the cell, we are scoped to the ListModel -->
<MenuItem Text="Delete" IsDestructive="True" Command="{Binding DeleteCommand}" />
<MenuItem Text="Delete" IsDestructive="True" Command="{Binding Path=BindingContext.DeleteCommand, Source={Reference ListOfPeople}}" CommandParameter="{Binding .}" />
<!-- Alternative way since Xamarin.Forms 4.3, using RelativeSource -->
<MenuItem Text="Delete" IsDestructive="True" Command="{Binding Source={RelativeSource AncestorType={x:Type local:MainPageViewModel}}, Path=DeleteCommand}" CommandParameter="{Binding .}" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment