Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Gesturerecognizer binding to command in complex layout

DinoNovak.3412DinoNovak.3412 USMember ✭✭
edited May 25 in Xamarin.Forms
 I have a complex page layout:
        <ContentPage.Content>
               <StackLayout >
                        <ScrollView >
                            <FlexLayout BindableLayout.ItemsSource="">
                                <BindableLayout.ItemTemplate>
                                    <DataTemplate>
                                                <components:SomeComponent />

                                    </DataTemplate>
                                </BindableLayout.ItemTemplate>
                            </FlexLayout>
                        </ScrollView>
                </StackLayout>
            </ContentPage.Content>

Then there is component definition:

    <StackLayout xmlns="http://xamarin.com/schemas/2014/forms"
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml x:Class="XXX.Components. SomeComponent">
            <AbsoluteLayout>
        .....
        </AbsoluteLayout>
        <StackLayout.GestureRecognizers>
            <TapGestureRecognizer Command="{Binding BindingContext.GoToDetailCommand}"
                                  CommandParameter="{Binding SomeID}"/>
        </StackLayout.GestureRecognizers>
    </StackLayout>

In my case I am unable to fire Tap gesture. I suspect that FlexLayout BindableLayout.ItemsSource="{Binding XXX}" is binding to specific observable collection, so in component Binding BindingContext.GoToDetailCommand actually does not exist (they are defined in initial viewmodel for this page).
Can you please advise how to sort this issue?

Dino

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    Firstly, name your component layout like:

    <StackLayout xmlns="http://xamarin.com/schemas/2014/forms"
                xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml 
                x:Class="XXX.Components. SomeComponent"
                x:Name="ParentLayout">
    

    And then change the binding like:

    <StackLayout.GestureRecognizers>
        <TapGestureRecognizer Command="{Binding Parent.BindingContext.GoToDetailCommand, Source={x:Reference ParentLayout}}"
                                CommandParameter="{Binding SomeID}"/>
    
    </StackLayout.GestureRecognizers>
    

    We could retrieve the parent FlexLayout that is located on the page through the parent property. Therefore, its binding context could also be accessed.

Sign In or Register to comment.