ListView没有列出数据

时间:2018-02-05 14:26:59

标签: xaml listview xamarin.forms

我试图让一个简单的ListView列出一些测试数据。但我必须忘记以某种方式绑定它。

视图模型:

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>

缺少什么?

1 个答案:

答案 0 :(得分:3)

使MoodEntries公开,你的绑定路径应该只是“EntryDate”,而不是“MoodEntries.EntryDate”

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