Xamarin.Forms.Behaviors icon indicating copy to clipboard operation
Xamarin.Forms.Behaviors copied to clipboard

EventToCommand on GestureRecognizers

Open alexandrereyes opened this issue 10 years ago • 5 comments

Trying to use EventToCommand with GestureRecognizers, no success with CommandParameter, here is the example:

<ListView.ItemTemplate> <DataTemplate> <ViewCell> <StackLayout Orientation="Horizontal" Padding="10" >

          <Label Text="{Binding NickName}">

            <Label.GestureRecognizers>
              <TapGestureRecognizer NumberOfTapsRequired="1">

                <b:Interaction.Behaviors>
                  <b:BehaviorCollection>
                    <b:EventToCommand CommandNameContext="{b:RelativeContext MainPage}"
                                      EventName="Tapped"
                                      CommandName="NickSelectedCommand"
                                      CommandParameter="{Binding NickName}" />
                  </b:BehaviorCollection>
                </b:Interaction.Behaviors>

              </TapGestureRecognizer>
            </Label.GestureRecognizers>

          </Label>

        </StackLayout>
      </ViewCell>
    </DataTemplate>
  </ListView.ItemTemplate>

If you took off the of command on ViewModel, it works The error is: System.ArgumentNullException: Argument cannot be null.

alexandrereyes avatar Oct 08 '14 12:10 alexandrereyes

Unfortunately TapGestureRecognizer doesn't inherit the BindingContext so EventToCommand's BindingContext is null and CommandParameter's binding fails, that's where the null comes from. Being part of GestureRecognizers collection there's no way to reach Label's BindingContext.

corcav avatar Oct 08 '14 16:10 corcav

Xamarin Forms limitation, I hope someday they can do something about.

Tks Corrado

Em 08/10/2014, à(s) 13:27, Corrado Cavalli [email protected] escreveu:

Unfortunately TapGestureRecognizer doesn't inherit the BindingContext so EventToCommand's BindingContext is null and CommandParameter's binding fails, that's where the null comes from. Being part of GestureRecognizers collection there's no way to reach Label's BindingContext.

— Reply to this email directly or view it on GitHub.

alexandrereyes avatar Oct 08 '14 17:10 alexandrereyes

Looks like, Xamarin Forms will support behaviors soon http://forums.xamarin.com/discussion/25612/xamarin-forms-1-3-0-technology-preview

Em 08/10/2014, à(s) 14:29, Alexandre [email protected] escreveu:

Xamarin Forms limitation, I hope someday they can do something about.

Tks Corrado

Em 08/10/2014, à(s) 13:27, Corrado Cavalli [email protected] escreveu:

Unfortunately TapGestureRecognizer doesn't inherit the BindingContext so EventToCommand's BindingContext is null and CommandParameter's binding fails, that's where the null comes from. Being part of GestureRecognizers collection there's no way to reach Label's BindingContext.

— Reply to this email directly or view it on GitHub.

alexandrereyes avatar Oct 08 '14 17:10 alexandrereyes

I see, cool, let's see their implementation...

corcav avatar Oct 09 '14 08:10 corcav

<Label.GestureRecognizers> <TapGestureRecognizer BindingContext="{behaviors:RelativeContext MyClassName}" Command="{Binding MyCmd}" </Label.GestureRecognizers>

rubgithub avatar Nov 04 '15 03:11 rubgithub