Give content page a name:
<ContentPage x:Name="this">
Access page's binding context like this:
<Label BindingContext="{Binding Source={x:Reference this}, Path=BindingContext}" >
DataContext.Command works for me.
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Name="firstPage"
x:Class="Your_Class_Name">
<ListView x:Name="ListSource" ItemsSource="{Binding ListSource}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid>
<Label Text="{Binding ABC}"></Label>
<Button Command="{Binding BindingContext.CommandFromParent, Source={x:Reference firstPage} }" />
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage>
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
You need to add BindingContext="{x:Reference viewmodel}
inside label.
<ViewCell>
<ViewCell.View>
<Label Text="{Binding ABC}" BindingContext="{x:Reference Name_Of_Parent}"></Label>
</ViewCell.View>
</ViewCell>
in Name_Of_Parent you put name of component. If you use MVVM and ViewModel class you have to add x:Name
to your binding Context:
<ContentPage.BindingContext>
<mvvm:MasterPageModel
x:Name="viewmodel"/>
</ContentPage.BindingContext>
This is documentation which describe it.
even qubuss give the correct answer I like to answer this question with example to make it more clear
lets consider we have a 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);
}
}
small Explanation of what happen , when we write BindingContext.CommandFromParent
BindingContext represent BindingContext of firstPage(x:Name="firstPage") which be ViewModelName