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 Objects in ListView Xamarin Forms

So here is my Model class

public class User{
    public String Name;
        public String Email;
}

So here is my 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="SampleApp.Screens.AdminApproveUsers"
             Title="Approve Users">

  <ScrollView>
    <ListView x:Name="lstUsers">
      <ListView.ItemTemplate>
        <DataTemplate>
          <ViewCell>
            <ViewCell.View>
              <StackLayout Orientation="Horizontal" VerticalOptions="Center" HorizontalOptions="Center">
                <Label Text="{Binding Name}" TextColor="Black" />
                <Label Text="{Binding Email}" TextColor="Black"  />
              </StackLayout>
            </ViewCell.View>
          </ViewCell>
        </DataTemplate>
      </ListView.ItemTemplate>
    </ListView>
  </ScrollView>
</ContentPage>

And this is how I bind it

public async void Initialize()
        {
            try
            {
                List<User> users = await ApiHandler.UserHandler.GetUsers();
                this.BindingContext = users;
                lstUsers.ItemsSource = users;

                lstUsers.ItemTapped += LstUsers_ItemTapped;
            }
            catch (Exception ex)
            {
                await DisplayAlert("Error", "Error Occurred", "OK");       
            }
        }

What am I doing wrong?

Best Answer

Answers

Sign In or Register to comment.