How to get BindingContext reference from a custom Cell?

tonholistonholis USMember ✭✭

Hi all,

Considering the XAML bellow, I have a page (StorePage) and ListView which has a DataTemplateSelector that renders different templates.
Inside I have a button that must call a command declared in the page's ViewModel.

The questions is: How can I get the page's BindingContext reference inside ?

Example:

<?xml version="1.0" encoding="UTF-8"?>

<ContentPage.Resources>
    <ResourceDictionary>
        <DataTemplate x:Key="ProductTemplate">
            <controls:ProductCell>
                <local:ProductListItemTemplate />
            </controls:ProductCell>
        </DataTemplate>
        <DataTemplate x:Key="MapTemplate">
            <ViewCell>
                <local:LocationTemplate />
            </ViewCell>
        </DataTemplate>

        <local:StoreTabTemplateSelector x:Key="TabTemplateSelector"
            Template1="{StaticResource ProductTemplate}"
            Template2="{StaticResource MapTemplate}"/>
    </ResourceDictionary>
</ContentPage.Resources>

<StackLayout Spacing="0">
    <ListView  x:Name="ListView"
        ItemsSource="{Binding Items}"
        ItemTemplate="{StaticResource TabTemplateSelector}"
        RefreshCommand="{Binding RefreshCommand}"
        IsRefreshing="{Binding IsRefreshing}"
        HasUnevenRows="True" 
        CachingStrategy="RecycleElement">
        <ListView.Behaviors>
            <b:EventToCommandBehavior EventName="ItemAppearing" 
                                              Command="{Binding LoadMoreCommand}" 
                                              EventArgsParameterPath="Item" />
        </ListView.Behaviors>
    </ListView>
</StackLayout>

Best Answer

  • tonholistonholis USMember ✭✭
    Accepted Answer

    Nevermind. I found a way to solve it, but I accept suggestions to improve the solutions.

    Here is what I did:

    In my page's class I created a static property to hold an instance of it.

    <?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:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
        xmlns:local="clr-namespace:MyApp.Views.Store;assembly=MyApp"
        prism:ViewModelLocator.AutowireViewModel="True"
        x:Class="MyApp.Views.MyPage"
        x:Name="MyPage">
    
        <ContentPage.Resources>
            <ResourceDictionary>
                <DataTemplate x:Key="ProductTemplate">
                    <controls:OfertaCell>
                        <local:ProductTemplate />
                    </controls:OfertaCell>
                </DataTemplate>
                <DataTemplate x:Key="MapTemplate">
                    <ViewCell>
                        <local:LocationTemplate />
                    </ViewCell>
                </DataTemplate>
    
                <local:StoreTabTemplateSelector x:Key="TabTemplateSelector"
                    Template1="{StaticResource ProductTemplate}"
                    Template2="{StaticResource MapTemplate}"/>
            </ResourceDictionary>
        </ContentPage.Resources>
    
        <StackLayout Spacing="0">
            <ListView  x:Name="ListView"
                ItemsSource="{Binding Items}"
                ItemTemplate="{StaticResource TabTemplateSelector}"
                HasUnevenRows="True" 
                CachingStrategy="RecycleElement">
            </ListView>
        </StackLayout>
    </ContentPage>
    
    
     public partial class MyPage : ContentPage
        {
            public MyPage()
            {
                InitializeComponent();
                Instance = this;
            }
    
            public static MyPage Instance { get; private set; }
        }
    

    In my ProductTemplate.xaml I accessed that

    <?xml version="1.0" encoding="UTF-8"?>
    <ContentView
        xmlns="http://xamarin.com/schemas/2014/forms"
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
        x:Class="TudousMobile.Consumidor.Views.Store.OfertaListItemTemplate"
        x:Name="MyContentView">
    
        <Button Text="View Item" 
                        Command="{Binding Path=ViewModel.ViewItemCommand, Source={x:Reference Name=MyContentView} }" 
                        CommandParameter="{Binding .}"/>
    
    </ContentView>
    
    
    public partial class ProductTemplate: ContentView
        {
            public ProductTemplate()
            {
                InitializeComponent();
                ViewModel = (MyPageViewModel)MyPage.Instance.BindingContext;
            }
    
            public MyPageViewModel ViewModel { get; private set; }
    
        }
    

Answers

  • tonholistonholis USMember ✭✭
    Accepted Answer

    Nevermind. I found a way to solve it, but I accept suggestions to improve the solutions.

    Here is what I did:

    In my page's class I created a static property to hold an instance of it.

    <?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:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
        xmlns:local="clr-namespace:MyApp.Views.Store;assembly=MyApp"
        prism:ViewModelLocator.AutowireViewModel="True"
        x:Class="MyApp.Views.MyPage"
        x:Name="MyPage">
    
        <ContentPage.Resources>
            <ResourceDictionary>
                <DataTemplate x:Key="ProductTemplate">
                    <controls:OfertaCell>
                        <local:ProductTemplate />
                    </controls:OfertaCell>
                </DataTemplate>
                <DataTemplate x:Key="MapTemplate">
                    <ViewCell>
                        <local:LocationTemplate />
                    </ViewCell>
                </DataTemplate>
    
                <local:StoreTabTemplateSelector x:Key="TabTemplateSelector"
                    Template1="{StaticResource ProductTemplate}"
                    Template2="{StaticResource MapTemplate}"/>
            </ResourceDictionary>
        </ContentPage.Resources>
    
        <StackLayout Spacing="0">
            <ListView  x:Name="ListView"
                ItemsSource="{Binding Items}"
                ItemTemplate="{StaticResource TabTemplateSelector}"
                HasUnevenRows="True" 
                CachingStrategy="RecycleElement">
            </ListView>
        </StackLayout>
    </ContentPage>
    
    
     public partial class MyPage : ContentPage
        {
            public MyPage()
            {
                InitializeComponent();
                Instance = this;
            }
    
            public static MyPage Instance { get; private set; }
        }
    

    In my ProductTemplate.xaml I accessed that

    <?xml version="1.0" encoding="UTF-8"?>
    <ContentView
        xmlns="http://xamarin.com/schemas/2014/forms"
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
        x:Class="TudousMobile.Consumidor.Views.Store.OfertaListItemTemplate"
        x:Name="MyContentView">
    
        <Button Text="View Item" 
                        Command="{Binding Path=ViewModel.ViewItemCommand, Source={x:Reference Name=MyContentView} }" 
                        CommandParameter="{Binding .}"/>
    
    </ContentView>
    
    
    public partial class ProductTemplate: ContentView
        {
            public ProductTemplate()
            {
                InitializeComponent();
                ViewModel = (MyPageViewModel)MyPage.Instance.BindingContext;
            }
    
            public MyPageViewModel ViewModel { get; private set; }
    
        }
    
Sign In or Register to comment.