Bug 44798 - forms Listview not showing items
Summary: forms Listview not showing items
Status: RESOLVED ANSWERED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.3.2
Hardware: All All
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2016-09-27 20:38 UTC by Andrew
Modified: 2016-10-10 15:07 UTC (History)
3 users (show)

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


Attachments
sample bug project (55.50 KB, application/zip)
2016-09-28 13:35 UTC, Andrew
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 ANSWERED

Description Andrew 2016-09-27 20:38:40 UTC
https://forums.xamarin.com/discussion/79193/forms-listview-not-showing/p1?new=1

code sample provided.  

Forms, ListView only showing last items in the list on Android and iOS.  

it works if you create the ItemTemplate like this (not like the one provided in code)

	list1.ItemTemplate = new DataTemplate(() =>
		   {
			   var label1 = new Label();
			   label1.FontAttributes = FontAttributes.Bold;
			   label1.TextColor = Color.Black;
			   label1.SetBinding(Label.TextProperty, "Subject");

			   var layout1 = new StackLayout();
			   layout1.Children.Add(label1);
			   return new ViewCell { View = layout1 };
			   
		   });


-------------------------------
Fails to bind this way:
--------------------------------
 public Page1()
        {
            var list1 = new ListView();
            //list1.HasUnevenRows = true;
            list1.IsGroupingEnabled = false;
            //list1.SetBinding(ListView.ItemsSourceProperty, "Model");

            var txtSubject = new Label
            {
                TextColor = Color.Black
            };

            txtSubject.SetBinding(Label.TextProperty, "Subject");


            list1.ItemTemplate = new DataTemplate(() =>
           {
               var view = new ViewCell();
               view.View = new StackLayout
               {
                   HeightRequest = 60,
                   Children =
                    {
                        txtSubject
                    }
               };
               return view;
           });

            var data2 = new List<Message>();
            data2.Add(new Message { Subject = "Test2" });
            data2.Add(new Message { Subject = "Test4" });
            data2.Add(new Message { Subject = "Test14" });

            list1.ItemsSource = data2;

            this.Content = list1;
        }

Model class.

    public class Message
    {
        public Message()
        {
        }

        public string Subject
        {
            get;
            set;
        }
    }
Comment 1 Andrew 2016-09-28 13:34:03 UTC
Sample instructions:

Open App.cs.

change this line:

// Bug
MainPage = new NavigationPage(new ErrorPage());

// Works
MainPage = new NavigationPage(new OKPage());

Xamarin.Forms v2.3.2.127  (confirmed) iOS and Android

Xamarin.Forms v2.3.3.152-pre2 (confirmed) iOS and Android
Comment 2 Andrew 2016-09-28 13:35:05 UTC
Created attachment 17785 [details]
sample bug project
Comment 3 E.Z. Hart [MSFT] 2016-10-10 15:07:15 UTC
In the non-working version of your code, you've created exactly one instance of the Label control (outside of your template); that same instance is used for every templated ListView item. The ViewCell for the first item ("Test2") is created and that single instance of Label is added to it. Then the ViewCell for the second item ("Test4") is created and the single Label instance is added to it (and removed from the "Test2" ViewCell, since a control can only exist in one place at a time). Finally, the ViewCell for the third item ("Test14") is created, and again the single instance of Label is added to that ViewCell (and removed from the "Test4" ViewCell). So only the last ViewCell created has a Label in it.

In your working version, you're creating a new instance of Label for each ViewCell, so each ViewCell has a control for displaying data. This is the correct way to create a DataTemplate.