Xamarin.forms: The EventToCommand behavior doesn't fire the button click event in list view

Created on 13 May 2019  路  3Comments  路  Source: xamarin/Xamarin.Forms

Description

The EventToCommand behavior doesn't trigger the button clicked event, in list view. In the attached sample we have a list view and added a grid as a DataTemplate for list view and i have added a button inside the grid and triggered the clicked event by using the EventToCommand behavior but the event doesn't fired.

Steps to Reproduce

  1. Run the attached sample EventToCommandSample
  2. Insert a break point on the ButtonClickedEvent method of MainPageViewModelClass
  3. Now tap the button on list view the method doesn't called

Expected Behavior

The button clicked event shout fired when we tap on the button.

Actual Behavior

The event doesn't fired as expected.

Basic Information

  • Version with issue: 3.6.0.x
  • Last known good version:
  • IDE: VS2015
  • Platform Target Frameworks:

    • Android: 8.1

  • Android Support Library Version:25.4.0.2
  • Nuget Packages: Xamarin.Forms, Prism
  • Affected Devices: Redmi Note 5

Reproduction Link

Sample link attached

unverified bug

Most helpful comment

This appears to be same issue as here: https://github.com/xamarin/Xamarin.Forms/issues/5414

The BindingContext of a cell is set to the relevant member of the ItemsSource. So, in the same way {Binding Name} is looking for the Name property on Person, {Binding ButtonClickedCommand} is looking for the ButtonClickedCommand property on Person.

One way to fix this is to add an x:Name to the page ( x:Name="Page" for instance) and change the binding to:

{Binding Source={x:Reference Page}, Path=BindingContext.ButtonClickedCommand}

This problem will still happen if you use the Button command directly (which you should probably do too).

All 3 comments

Out of curiosity, why don't you use the button command property instead of the behavior?

<Button Text="{Binding Name}" TextColor="#000000" InputTransparent="False" Command="{Binding ButtonClickedCommand}" />

This appears to be same issue as here: https://github.com/xamarin/Xamarin.Forms/issues/5414

The BindingContext of a cell is set to the relevant member of the ItemsSource. So, in the same way {Binding Name} is looking for the Name property on Person, {Binding ButtonClickedCommand} is looking for the ButtonClickedCommand property on Person.

One way to fix this is to add an x:Name to the page ( x:Name="Page" for instance) and change the binding to:

{Binding Source={x:Reference Page}, Path=BindingContext.ButtonClickedCommand}

This problem will still happen if you use the Button command directly (which you should probably do too).

@GalaxiaGuy Thank you for your suggestion it works as expected after applying your suggestion. Thanks for your help.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hartez picture hartez  路  178Comments

kingces95 picture kingces95  路  89Comments

mfeingol picture mfeingol  路  58Comments

dan5602 picture dan5602  路  124Comments

jassmith picture jassmith  路  199Comments