I'm binding a customer list to a Xamarin Listview. I want to use a DataTemplate with a ViewCell in it.
As per documentation, we can use one of 2 constructors:
new DataTemplate(typeof(CustomerViewCell));
new DataTemplate(=>new CustomerViewCell);
However, they lead to different results. The first one correctly displays the list, while the second one shows repeated the last item of the list.
Do I don't know something basic here?
Here's my customer model:
public class Customers : List<Customer>
{
public Customers()
{
Add(new Customer { No = 1, Name = "Microsoft" });
Add(new Customer { No = 2, Name = "Google" });
Add(new Customer { No = 3, Name = "Facebook" });
}
}
public class Customer {
public int No { get; set; }
public string Name { get; set; }
}
And here is the CustomerViewCell.xaml, which is inherited from ViewCell
<?xml version="1.0" encoding="UTF-8"?>
<ViewCell xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="LVDataTemplate.View.CustomerViewCell">
<ViewCell.View>
<StackLayout Orientation="Horizontal">
<Label Text="{Binding No}" />
<Label Text="{Binding Name}" />
</StackLayout>
</ViewCell.View>
</ViewCell>
The CustomerListView, which shows the Customers:
public partial class CustomerListView :ContentPage
{
public CustomerListView()
{
InitializeComponent();
MyListView.ItemsSource = new Customers();
//1. Work correctly
// MyListView.ItemTemplate = new DataTemplate(typeof(CustomerViewCell));
//2. Work in-correctly. only the last item repeated through out the list
var theCell = new CustomerViewCell();
MyListView.ItemTemplate = new DataTemplate(()=>theCell);
}
}
The first code shows the result:
- Microsoft.
The second shows