Forum Xamarin.Forms

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Unable to bind data to ListView

Hi, I have been trying to bind data to a ListView, but the data doesn't show up on the UI.

Below is the code

<?xml version="1.0" encoding="utf-8"?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:ToDoList" x:Class="ToDoList.ToDoListPage" x:Name="ToDoPage" Title="Hello World!"> <StackLayout VerticalOptions="FillAndExpand" Orientation="Vertical"> <ListView x:Name="ListViewEventsToDo"> <ListView.ItemTemplate> <DataTemplate> <TextCell Text="{Binding Title}" Detail="{Binding Description}" /> </DataTemplate> </ListView.ItemTemplate> </ListView> </StackLayout> </ContentPage>

The code behind for this:

public enum HappeningType { Birthday, Meeting, Chore, Other }

public class Happening
{
    public string Title { private get; set; }

    public string Description { private get; set; }

    public HappeningType HappenningType
    {
        private get;
        set;
    }

    public Happening(string title,  string description, HappeningType happeningType)
    {
        this.Title = title;
        this.Description = description;
        this.HappenningType = happeningType;
    }
}

public partial class ToDoListPage : ContentPage
{
    ObservableCollection<Happening> happenings = new ObservableCollection<Happening>();

    public ToDoListPage()
    {
        InitializeComponent();

        happenings = new ObservableCollection<Happening>
        {
            new Happening("Cathy's Birthday", "Buy some gift", HappeningType.Birthday),
            new Happening("Team Meeting", "Let's meet up for the Daily Scrum", HappeningType.Meeting),
            new Happening("Buy Groceries", "to buy groceries post dinner", HappeningType.Other)
        };

        ListViewEventsToDo.ItemsSource = happenings;

    }
}

`

Could someone help me out?

Best Answer

Answers

  • csreeharicsreehari USMember
    edited February 2017

    Oh! That's a quite a big blunder on getters of the properties. Thanks a lot @AlexDunn. But could you also please elaborate on that Setbinding method, I didn't get the context on how to use it along with setting the ItemsSource.

Sign In or Register to comment.