Last active
          March 30, 2020 20:20 
        
      - 
      
- 
        Save jfversluis/810462d21a525b8e9dfe81f6f225e8fe to your computer and use it in GitHub Desktop. 
    Page with ListView with differend data binding options
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | <?xml version="1.0" encoding="utf-8"?> | |
| <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:d="http://xamarin.com/schemas/2014/forms/design" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 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"> | |
| <ListView.ItemTemplate> | |
| <DataTemplate> | |
| <!-- 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}"> | |
| <!-- WARNING: NOT WORKING! --> | |
| <!-- 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 --> | |
| <!--<ImageCell.ContextActions> | |
| <MenuItem Text="Delete" IsDestructive="True" Command="{Binding DeleteCommand}" /> | |
| </ImageCell.ContextActions>--> | |
| <ImageCell.ContextActions> | |
| <MenuItem Text="Delete" IsDestructive="True" Command="{Binding Path=BindingContext.DeleteCommand, Source={Reference ListOfPeople}}" CommandParameter="{Binding .}" /> | |
| </ImageCell.ContextActions> | |
| <!-- Alternative way since Xamarin.Forms 4.3, using RelativeSource --> | |
| <!--<ImageCell.ContextActions> | |
| <MenuItem Text="Delete" IsDestructive="True" Command="{Binding Source={RelativeSource AncestorType={x:Type local:MainPageViewModel}}, Path=DeleteCommand}" CommandParameter="{Binding .}" /> | |
| </ImageCell.ContextActions>--> | |
| </ImageCell> | |
| </DataTemplate> | |
| </ListView.ItemTemplate> | |
| </ListView> | |
| </ContentPage> | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment