Bug 21501 - [Android only] ListView: Button in ItemTemplate breaks SelectedItem
Summary: [Android only] ListView: Button in ItemTemplate breaks SelectedItem
Status: RESOLVED FIXED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 1.2.2
Hardware: PC Windows
: Normal normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2014-07-22 05:52 UTC by Daniel Münch
Modified: 2015-01-18 04:09 UTC (History)
9 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Test case XAML and code behind (1.18 KB, application/zip)
2014-07-22 05:52 UTC, Daniel Münch
Details
Test case: whole solution (93.73 KB, application/zip)
2014-09-11 01:43 UTC, Brendan Zagaeski (Xamarin Team, assistant)
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Daniel Münch 2014-07-22 05:52:28 UTC
Created attachment 7441 [details]
Test case XAML and code behind

If I use a Button element in the ItemTemplate of a ListView, the bound SelectedItem property in my ViewModel isn't set anymore on list item selection. Guess the Button eats up all the click events.

Attached XAML code with code behind and view model.
ListView: Button in ItemTemplate breakes SelectedItem.


On Xamarin.Forms 1.2.2 pre-release.
Comment 1 Daniel Münch 2014-07-22 06:12:49 UTC
Forgot to mention, this is on Android 4.3 API 18.
Comment 2 Devin Rose 2014-08-05 10:42:53 UTC
I get this same bug. Sample page code below:

List stringList = new List () { "abc", "xyz", "todo" };

        _listView = new ListView ();
        _listView.ItemsSource = stringList;

        _listView.ItemTemplate = new DataTemplate(() =>
            {
                var label = new Label();
                label.SetBinding(Label.TextProperty, ".");

                var button = new Button() { Text = "Test" };

                return new ViewCell
                {
                    View = new StackLayout
                    {
                        Padding = new Thickness(0, 5),
                        Orientation = StackOrientation.Horizontal,
                        Children =  { label, button }
                    }
                };
            });


        var resultLabel = new Label() { Text = "A" };
        _listView.ItemSelected += (sender, args) =>
        {
            resultLabel.Text = resultLabel.Text + "!";
            //Navigation.PushAsync(new OtherPage());
        };


        var layout = new StackLayout () {
            Orientation = StackOrientation.Vertical,
            Children = { _listView, resultLabel }
        };

        Content = layout;
Comment 3 Arpit Jha 2014-08-21 02:08:16 UTC
I have checked this issue and able to reproduce it.

Steps to reproduce this issue.
1.Create a xamarin Forms PCL Application
2.Add a Class(.Xaml) and Add above code and did some modification in it.
3.Run the application

I observed that the listview  having a button in Dataitem template and when I clicked on  button,its breaking selected item.

Screencast : http://screencast.com/t/WLwhPjyl

Environment Info: 
XVS 3.3.47.0

Xamarin Forms Version : 1.2.2.6243
Comment 4 Brendan Zagaeski (Xamarin Team, assistant) 2014-09-11 01:43:02 UTC
Created attachment 7994 [details]
Test case: whole solution

Here's another version of the test case, this time wrapped up in a solution for convenient testing.

This test case assigns Clicked event handlers to the buttons to help demonstrate how the tap events behave. This test case also happens to use ItemTapped rather than ItemSelected or SelectedItem, but I don't think that matters much.


One problem with this bug is that it depends on a particular "special" view hierarchy. Each platform has its own idiosyncrasies about focusing items and propagating events for view hierarchies. It might be possible to make Xamarin.Forms behave identically across all 3 platforms for all view hierarchies, but it also might _not_ be possible to prevent inconsistencies altogether.

Even if a general fix isn't possible, having a built-in fix for this particular case would be nice because it's fairly common to include buttons in list items.


## Steps to reproduce

1. Launch the app.

2. Tap each of the 4 list items.


## Result on Android (Problematic result)

- The two list items _with_ buttons do not respond to taps (unless the button within the list item is tapped).

- The two list items _without_ buttons successfully open the "Item Tapped" alert.

- Both buttons open the "Button Tapped" alert.


## Result on iOS ("Good" result)

- All four list items open the "Item Tapped" alert.

- Both buttons open the "Button Tapped" alert.

(These are probably the expected results for most users.)


## Result on Windows Phone 8.0 emulator

- All four list items open the "Item Tapped" alert.

- Both buttons "incorrectly" open the "Item Tapped" alert. This is a separate issue (bug 22854).


## Version information

Xamarin.Forms 1.2.3.6246-pre1
Xamarin Support Library v4 20.0.0.3
Windows Phone Toolkit 4.2013.08.16

Xamarin 3.5.57
Comment 6 Jason Smith [MSFT] 2015-01-18 04:09:19 UTC
Should be fixed in 1.3.0