Bug 48271 - ListView with custom cell causes selected item to be null
Summary: ListView with custom cell causes selected item to be null
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.3.3
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2016-11-29 12:39 UTC by ThomasWendling
Modified: 2016-12-08 07:43 UTC (History)
3 users (show)

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

Sample VS2012 project "ListViewCustomCellProj" (150.39 KB, application/octet-stream)
2016-11-30 14:49 UTC, ThomasWendling

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:

Description ThomasWendling 2016-11-29 12:39:46 UTC

I implemented a custom view cell for the ListView DataTemplate as described here: https://developer.xamarin.com/guides/xamarin-forms/custom-renderer/viewcell/

It works great, but when I select an item in the ListView, the selected item value - bound to a ViewModel property - is null. Before I switched over to the custom cell, I used a default ViewCell, which works great. Selecting a row in the ListView raises an event and so I got the correct row object in the ViewModel.

After many hours of researching I finally found a possible error:
I used the CachingStrategy "RecycleElement" in the ListView, which causes the selected item to be null after forwarding it to the ViewModel. After setting the CachingStrategy to the default value "RetainElement", then everything works fine. This error only occurrs, if I use a custom cell.

I'm using the latest stable versions of Xamarin.iOS with Xamarin.Forms
Comment 1 adrianknight89 2016-11-29 22:40:13 UTC
Can you attach a repro?
Comment 2 ThomasWendling 2016-11-30 14:49:34 UTC
Created attachment 18718 [details]
Sample VS2012 project "ListViewCustomCellProj"

Set a breakpoint into the method 
public void OnItemSelected(object sender, SelectedItemChangedEventArgs e) 
in the code behind file MainPage.xaml.cs in the PCL. The propety "Item" in the SelectedItemChangedEventArgs will be null. Change the CachingStrategy of the ListView from "RecycleElement" to "RetainElement" and it works.
Comment 3 adrianknight89 2016-12-03 21:13:05 UTC
See https://github.com/xamarin/Xamarin.Forms/pull/598
Comment 4 adrianknight89 2016-12-04 18:45:59 UTC
You could see the changes I made in that PR; however, I recommended an update on the tutorial that you were following since it doesn't seem to be targeted to RecycleElement. It could help us understand the issue.

Also, I'm not sure if going through custom renderers is a good idea. In your ListView ItemTemplate, why don't you create the labels in XAML there? https://developer.xamarin.com/guides/xamarin-forms/templates/data-templates/creating/

Overriding GetCell has present and future complications. For example, you're not doing the things XF is doing: https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Platform.iOS/Cells/ViewCellRenderer.cs#L11

I'd avoid custom renderers as much as possible.
Comment 5 ThomasWendling 2016-12-05 08:03:30 UTC
Hi Adrian,

thanks for your suggestions, I really appreciate that. 

I thought, I follow the official Xamarin Forms recommendations:

"Consider a custom renderer if you need a very specific, complex design presented in your cells."

I'm using the custom cell and the platform specific renderer in only one specific case in my app, where a default ViewCell in the ItemTemplate works too slow for me, especially when I scroll through the ListView.

Comment 6 adrianknight89 2016-12-05 15:27:24 UTC
I agree that custom renderers should be used in specific, complex cases, but this requires you to have knowledge of individual platforms and the XF code you're bypassing. If you think ViewCell is too slow, maybe you can submit another bug with a minimal repro for us to figure out why it's slow.
Comment 7 ThomasWendling 2016-12-08 07:43:27 UTC
Thanks a lot! :-)
I'll submit another bug with a repro in a few days.