Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my listview appear to be empty?

I am new to Xamarin Forms and XAML and I would like to fill a <ListView> with appointments. My XAML looks like this:

<ListView x:Name="appointmentsView"
          ItemsSource="{Binding appointmentList}" 
          Grid.Row="1"
          AbsoluteLayout.LayoutBounds="0.50, 0.2, 1, 0.5"
          AbsoluteLayout.LayoutFlags="All">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell> 
                <ViewCell.View>
                    <Label TextColor="Black" Text="{Binding app1}"></Label>
                </ViewCell.View>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

And the code looks like this:

public partial class Home : ContentPage
{
    public ObservableCollection<Appointment> appointmentList = new ObservableCollection<Appointment> ();

    public Employee Tom { get; set; }
    public Appointment app1 { get; set; }

    public Home ()
    {
        InitializeComponent ();

        Tom = new Employee("Tom", "Tommen");
        app1 = new Appointment(new DateTime(), Tom);

        appointmentList.Add(app1);
        appointmentsView.ItemsSource = appointmentList;
    }

There is nothing in the list when I debug the application, I would really appreciate it if somebody could tell me what I am doing wrong!

like image 940
Eleina Avatar asked Oct 19 '22 01:10

Eleina


1 Answers

It really depends on how Employee and Appointment are defined. For instance:

public class Appointment
{
    public DateTime AppointmentDateTime { get; set; }
    public Employee AppointmentEmployee { get; set; }

    public Appointment(DateTime AppointmentDateTime, Employee AppointmentEmployee)
    {
        this.AppointmentDateTime = AppointmentDateTime;
        this.AppointmentEmployee = AppointmentEmployee;
    }
}

public class Employee
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public Employee(string FirstName, string LastName)
    {
        this.FirstName = FirstName;
        this.LastName = LastName;
    }

    override public string ToString()
    {
        return string.Format("{0}, {1}", LastName, FirstName);
    }
}

Then your XAML should be:

<ListView x:Name="appointmentsView"
      ItemsSource="{Binding appointmentList}" 
      Grid.Row="1">
        <ListView.ItemTemplate>
        <DataTemplate>
                <Label Text="{Binding AppointmentEmployee}"></Label>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

enter image description here

like image 94
jsanalytics Avatar answered Nov 15 '22 04:11

jsanalytics