I have a problem where I'm trying to display data inside a ListView in my app when binding a list of data to my ListView in XAML. The ListView does not populate with any data at all.
Here is all my coding:
My Code-behind
public ObservableCollection<RandomList> randomList = new ObservableCollection<RandomList>();
public App()
{
MainPage = new MainUI();
randomList.Add(new RandomList { Name = "Susan", Surname = "Potgieter", School = "Oosterlig" });
randomList.Add(new RandomList { Name = "Jonathan", Surname = "Visagie", School = "Elspark" });
randomList.Add(new RandomList { Name = "Gerhard", Surname = "Vermaak", School = "E.G. Jansen" });
randomList.Add(new RandomList { Name = "Kobus", Surname = "Jacobs", School = "Oosterlig" });
}
My XAML
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="RandomTest.MainUI">
<StackLayout Padding="0,40,0,0">
<Label Text="Hello" VerticalOptions="Center" HorizontalOptions="Center" />
<Button x:Name="btnGetStudents" Text="Get Students"/>
<ListView x:Name="lwStudents" ItemsSource="{Binding randomList}">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding Name}" Detail="{Binding Surname}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage>
When I insert a breakpoint in my code behind where I add the dummy data to the list, I can then see in my XAML here: <ListView x:Name="lwStudents" ItemsSource="{Binding randomList}"> that there are 4 items in my randomList.
The problem is that there is no data in the two bindings below:
<TextCell Text="{Binding Name}" Detail="{Binding Surname}"/>
Can someone please tell or show me what I'm doing wrong in my binding? Or if you need ANY more info or code, please ask! I will be happy to give more info. :)
EDIT
I've updated my RandomList class and implemented INotifyPropertyChanged, but I still can't get my data to load in my ListView through XAML bindings.
using System.ComponentModel;
namespace RandomTest
{
public class RandomList : INotifyPropertyChanged
{
public int Id { get; set; }
string _name;
public string Name
{
get { return _name; }
set
{
if (_name != value)
_name = value;
OnPropertyChanged("Name");
}
}
string _surname;
public string Surname
{
get { return _surname; }
set
{
if (_surname != value)
_surname = value;
OnPropertyChanged("Surname");
}
}
string _school;
public string School
{
get { return _school; }
set
{
if (_school != value)
_school = value;
OnPropertyChanged("School");
}
}
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
EDIT 2
This is how my MainUI.xaml.cs page looks like at the moment:
public partial class MainUI : ContentPage
{
public MainUI()
{
InitializeComponent();
BindingContext = new MainUIModel();
MainUIModel mainUIModel = (MainUIModel)this.BindingContext;
mainUIModel.randomList.Add(new RandomList { Name = "Susan", Surname = "Potgieter", School = "Oosterlig" });
mainUIModel.randomList.Add(new RandomList { Name = "Jonathan", Surname = "Visagie", School = "Elspark" });
mainUIModel.randomList.Add(new RandomList { Name = "Gerhard", Surname = "Vermaak", School = "E.G. Jansen" });
mainUIModel.randomList.Add(new RandomList { Name = "Kobus", Surname = "Jacobs", School = "Oosterlig" });
}
}
INotifyPropertyChangedinherited in your model which you might have set asBindingContext?INotifyPropertyChangedfor the class. Do you think it could help? It's strange because I can set the itemsource of the listview in code and it displays all the data, but it doesn't work in XAML... :|INotifyPropertyChangedin the class and let you know if it works. Thanks!INotifyPropertyChangedand it still doesn't return any data. Could the binding in XAML be wrong?