haierophanto
haierophanto

Reputation: 51

How to show a list of data in XAML?

The point here is that I want to replicate the following example within XAML-page. Difference here is that I don't know beforehand the quantity of data to show, so it has to be kind of generic one.

Let's say, I have some model UserModel.cs which looks like this:

class UserModel
{
    string FirstName { get; set; }
    int BirthYear { get; set; }
}

and I have some view DataViewer.cs which looks like this:

class DataViewer
{
    static void ShowData()
    {
        var users = new List<UserModel>() 
        {
            new UserModel() { FirstName = "Thomas", BirthYear = 1995 },
            new UserModel() { FirstName = "Arthur", BirthYear = 1991 },
            new UserModel() { FirstName = "John", BirthYear = 2000 }
        };

        foreach (var user in users)
        {
            Console.WriteLine($"{user.FirstName} was born in {user.BirthYear}");
        }
    }
}

Upvotes: 0

Views: 1082

Answers (1)

Nk54
Nk54

Reputation: 771

Just follow the given exemple on the documentation which you can find here. I can't help you more that the official documentation as it is very solid on CollectionView : a lot of exemple and how to make it working.

Anyway here is a starting point :

<CollectionView ItemsSource="{Binding Users}">
    <CollectionView.ItemTemplate>
        <DataTemplate>
            <StackLayout Orientation="Horizontal">
                <Label Text="{Binding FirstName}" />
                <Label Text="{Binding BirthYear}" />
            </StackLayout>
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

Assuming you have a collection property called Users in your ViewModel.

Upvotes: 2

Related Questions