Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

I'm struggling to get databinding to work

$
0
0

So I am pulling from a table called "Person" in a SQLite database. This table has several properties including the PersonId and PersonName. There are many "people" in this table. I am trying to pull a specific PersonName and populate a Button's text with it. What is coming out is just a blank button with no text. Here is what I have in xaml:

<ContentPage.Content>
    <ListView x:Name="peopleList">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell Height="30">
                    <StackLayout Padding="5">
                        <Button Text="{Binding People[0].PersonName}" />
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</ContentPage.Content>

Here is the code behind:

public List<Person> People { get; set; }

    protected override void OnAppearing()
{
    PopulatePeople();

}



    public async void PopulatePeople()
    {
        List<Person> People = await App.PeopleRepo.GetAllPeople();
            peopleList.ItemsSource = People; 
     }

Here is the GetAllPeople() method defined in the PeopleRepository class:

    public SQLiteAsyncConnection conn;
    public PeopleRepository(string db)
    {
        conn = new SQLiteAsyncConnection(db);
        conn.CreateTableAsync<Person>().Wait();
    }

        public async Task<List<Person>> GetAllPeople()
    {
        try
        {
             return await conn.Table<Person>().ToListAsync();
        }
        catch (Exception ex)
        {
            Status = string.Format("Failed to retrieve data. {0}", ex.Message);
        }

        return new List<Person>();
    }

I just can't get this working. I have struggled some with my understanding of databinding and have researched it a ton and I feel that this should work. What am I missing?


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>