Forum Xamarin.Forms

Relative context for tap gestures

I want to bind to command from context outside of my data template. I may do it using Button control and EventToCommand behavior. However I haven't found a way to do it for tap gestures.

Here is a small example when I want to present a small tree structure of tags (tag only has name and children properties). My intention is to go to edit page after clicking tag name without a need to have special button

    <ContentPage xmlns=""
          <DataTemplate x:Key="Tag">
            <StackLayout Orientation="Vertical">          
              <StackLayout Orientation="Horizontal">
                <Label Text="{Binding Name}" Margin="2">
                    <TapGestureRecognizer Command="{Binding GotoEditTagCommand}" CommandParameter="{Binding }"></TapGestureRecognizer>
                <Button Text="Edit">
                    <mvvm:EventToCommandBehavior CommandNameContext="{mvvm:RelativeContext TagPage}" CommandName="GotoEditTagCommand" CommandParameter="{Binding }" EventName="Clicked" />
              <controls:RepeaterView ItemsSource="{Binding Children}" ItemTemplate="{StaticResource Tag}" Margin="15, 2, 2, 2" />
      <controls:RepeaterView ItemsSource="{Binding Tags}" Spacing="2" ItemTemplate="{StaticResource Tag}">    


Any ideas how to make it work also for tap gestures?


  • ZoliZoli NLMember ✭✭✭

    No solution yet?
    I have a listview with image, and want to bind the TapGestureRecognizer command of the image to a command outside of the listview.item context (to the main viewmodel, which contains the listview)

  • jjtudujjtudu Member ✭✭
    <DataTemplate x:Key="examTemplate">
         <ImageCell Command="{Binding Source= {x:Reference PageName},Path=BindingContext.NavigateCommand}"
                             CommandParameter="{Binding .}" 
                             Text="{Binding name}" Detail="{Binding time}"/>                    

    Replace PageName with Your list name, where itemsource is binded, i prefer PageName.
    CommandParameter will have item DataContext, but Command will have Outer Datacontext

Sign In or Register to comment.