I have a problem when I try to display data internally ListViewin my application, when I bind a list of data to my list in XAML. ListView does not populate with any data whatsoever.
Here is all my coding:
My code
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, where I add dummy data to the list, I can see my XAML: here <ListView x:Name="lwStudents" ItemsSource="{Binding randomList}">that randomListthere are 4 elements in mine .
The problem is that in the two bindings there is no data below:
<TextCell Text="{Binding Name}" Detail="{Binding Surname}"/>
Can someone tell or show me what I'm doing wrong in my binding? Or if you need more information or a code, please ask! I will be happy to provide more information. :)
EDIT
randomList INotifyPropertyChanged, ListView XAML.
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));
}
}
}
2
MainUI.xaml.cs :
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" });
}
}