Lasse Edsvik
Lasse Edsvik

Reputation: 9298

ListView not listing data

I'm trying to get a simple ListView to list some test data. But I must have forgotten to bind it somehow.

ViewModel:

public class EntriesViewModel
{
    List<MoodEntry> MoodEntries { get; set; }

    public EntriesViewModel()
    {
        MoodEntries = App.MoodDatabase.GetMoodEntries();

        // Add test data
        MoodEntries.Add(new MoodEntry {
            EntryDate = DateTime.Today,
            Stress = 3,
            AchivedGoals = 3,
            Sleep = 3
        });
    }
}

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="myMood.Views.Entries"
             Icon="ic_view_headline_white_24dp.png"
             xmlns:viewModels="clr-namespace:myMood.ViewModels">
    <ContentPage.BindingContext>
        <viewModels:EntriesViewModel />
    </ContentPage.BindingContext>

    <ListView ItemsSource="{Binding MoodEntries}" 
              HasUnevenRows="True"
              Margin="20">
        <ListView.ItemTemplate>
            <DataTemplate>                
                <ViewCell>
                    <Label Text="{Binding MoodEntries.EntryDate}"></Label>
                </ViewCell>                
            </DataTemplate>            
        </ListView.ItemTemplate>
    </ListView>    
</ContentPage>

What's missing?

Upvotes: 1

Views: 24

Answers (1)

Jason
Jason

Reputation: 89204

make MoodEntries public, and your binding path should just be "EntryDate", not "MoodEntries.EntryDate"

<Label Text="{Binding EntryDate}"></Label>

Upvotes: 3

Related Questions