Using x:Reference from inside a DataTemplate stored in another XAML file

SergiuCosminSergiuCosmin USMember ✭✭
edited February 2016 in Xamarin.Forms

hello,

so I have a app using Prism as my MVVM engine and one of the pages is a MasterDetail page in which the details page part looks like this

<MasterDetailPage.Detail> <NavigationPage> <x:Arguments> <StackLayout Padding="10, 0, 0, 0"> <ListView x:Name="listView" IsPullToRefreshEnabled="False" CachingStrategy="RecycleElement" ItemsSource="{Binding Products}"> <ListView.ItemTemplate> <DataTemplate> <ViewCell> <StackLayout> <StackLayout.GestureRecognizers> <TapGestureRecognizer Tapped="OnItemTapped" Command="{Binding Source={x:Reference listView}, Path=BindingContext.ClickProductTreeCommand}" CommandParameter="{Binding}"/> </StackLayout.GestureRecognizers> <StackLayout.Padding> <OnPlatform x:TypeArguments="Thickness" iOS="5,0,0,0"/> </StackLayout.Padding> <Label FontSize="Medium" FontAttributes="Bold" VerticalOptions="CenterAndExpand" HorizontalOptions="Start" Text="{Binding Name}"/> </StackLayout> </ViewCell> </DataTemplate> </ListView.ItemTemplate> </ListView> </StackLayout> </x:Arguments> </NavigationPage> </MasterDetailPage.Detail>

everything works fine, but now it has come up that some list view items will need an extra label and a button so I decided to update to XF 2.1.0 pre 5 and test the new DataTemplateSelector they just introduced, so I made all the necessary arrangements and made the C# class and two new xaml files with the appropriate data templates, the above one looks like so:

<?xml version="1.0" encoding="utf-8" ?> <ViewCell xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="TestApp.Views.MenuItemCellTemplate"> <StackLayout> <StackLayout.GestureRecognizers> <TapGestureRecognizer Command="{Binding Source={x:Reference listView}, Path=BindingContext.ClickProductTreeCommand}" CommandParameter="{Binding}"/> </StackLayout.GestureRecognizers> <StackLayout.Padding> <OnPlatform x:TypeArguments="Thickness" iOS="5,0,0,0"/> </StackLayout.Padding> <Label FontSize="Medium" FontAttributes="Bold" VerticalOptions="CenterAndExpand" HorizontalOptions="Start" Text="{Binding Name}"/> </StackLayout> </ViewCell>

and the MasterDetail page now looks like:

... <ListView x:Name="listView" IsPullToRefreshEnabled="False" CachingStrategy="RecycleElement" ItemsSource="{Binding Products}"> <ListView.ItemTemplate> <template:ProductMenuDataTemplateSelector/> </ListView.ItemTemplate> </ListView> ...

but now I get an error when I get to this page :

System.Exception: Can't resolve name on Element

so my guess was automatically that it cannot find the x:Reference part namely the listView element, and so I commented the TapGestureRecognizer part of the code to test my theory and apparently I was right, with that piece of code commented out there is no error anymore but also I don't have the command implemented

so my question is how can I access the command in the MasterDetail viewmodel as clearly the x:Reference does not work in this situation and I don't think there is an implementation of a AncestorType in XF at this time, also pondering on this problem I have another question: if I had the master and detail pages in different XAML pages how could they access the viewmodel from the MasterDetail page ?

thank you in advance for any ideeas

Answers

  • MichaelDimoudisMichaelDimoudis AUUniversity ✭✭

    Hi I have the exact same question. Did you solve this?

  • SergiuCosminSergiuCosmin USMember ✭✭

    @MichaelDimoudis said:
    Hi I have the exact same question. Did you solve this?

    hi,

    sorry for the late reply, what I ended up doing is send the master detail view model in the child view model and then just access the command of the parent like so

    XAML <StackLayout.GestureRecognizers> <TapGestureRecognizer Command="{Binding Parent.TapMenuItemCommand}" CommandParameter="{Binding}"/> </StackLayout.GestureRecognizers>

Sign In or Register to comment.