Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin form: How to reference to the parent binding

<ViewCell>     <ViewCell.View>       <Label Text="{Binding ABC}"></Label>    </ViewCell.View> </ViewCell> 

Assuming this viewcell was inside ListView. If the content page was binding with a view model, how can I get a reference to the content page's binding. Currently, 'ABC' is referencing the property of an object in the list but i want to get the value from the content page's bindingcontext.

<ffimageloading:CachedImage.GestureRecognizers>    <TapGestureRecognizer BindingContext="{x:Reference page}" Command="{Binding OnSignInCommand}" CommandParameter="{Binding Model}" /> </ffimageloading:CachedImage.GestureRecognizers> 
like image 518
LittleFunny Avatar asked Jan 18 '18 06:01

LittleFunny


2 Answers

@qubuss gave the correct answer below, but I would like to give more context and show an example to make it more clear:

Lets consider the following page:

<ContentPage       xmlns="http://xamarin.com/schemas/2014/forms"      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"     x:Name="firstPage" -->this reference parent context     x:Class="Your_Class_Name">   <ListView x:Name="ListSource"             ItemsSource="{Binding ListSource}" >             <ListView.ItemTemplate>                <DataTemplate>                    <ViewCell>                         <Grid>                          // this come from item source                     <Label Text="{Binding ABC}"></Label>                     <Button Command="{Binding BindingContext.CommandFromParent                            , Source={x:Reference firstPage} }" />                         </Grid>                                                                         </ViewCell>                   /DataTemplate>            </ListView.ItemTemplate>     </ListView>   </ContentPage> 

your view Model should look like that

 public class ViewModelName      {         private List<YourDataType> _listSource = new List<YourDataType>();                   public List<YourDataType> ListSource         {             get => _listSource;             set             {                 _listSource = value;                 RaisePropertyChanged();             }         }          public ICommand CommandFromParent => new Command(HandleYourActionHere);  } } 

Explanation When we write BindingContext.CommandFromParent, BindingContext represents the BindingContext of firstPage(x:Name="firstPage") which is of type ViewModelName

like image 146
Mina Fawzy Avatar answered Oct 07 '22 10:10

Mina Fawzy


You can bind to an ancestor with a RelativeSource.

Change this line

<Label Text="{Binding ABC}"></Label> 

to this

<Label Text="{Binding ABC, Source={RelativeSource AncestorType={x:Type viewModel:YourViewModelName}}}"></Label> 

Don't forget to add the xml namespace for viewModel on top of the file:

xmlns:viewModel="clr-namespace:YourProject.ViewModels" 

You can read all about RelativeSource bindings here: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data-binding/relative-bindings#bind-to-an-ancestor

like image 24
jfmg Avatar answered Oct 07 '22 10:10

jfmg