0

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}");
        }
    }
}
  • 1
    You could place a `datagrid` inside your `XAML` and then add rows/columns to it dynamically. This is a post with information on adding the rows/columns dynamically: (https://stackoverflow.com/questions/704724/programmatically-add-column-rows-to-wpf-datagrid) – Ryan Wilson Apr 17 '20 at 21:21
  • I don't fully understand your need. Do you want to show a list of UserModel or a list of different type of data ? Exemple : a list composed of UserModel and CustomerModel and any other Model you might have ? Or you need to display a collection which might be updated in the future and want that changes to be reflected on the user interface ? – Nk54 Apr 18 '20 at 09:27
  • @Nk54 I want to have CollectionView which will be populated with data from database. Let's assume that I want to grab `UsersModels` from DB and display their names on my page – haierophanto Apr 18 '20 at 11:56
  • Is it CollectionView from Xamarin Forms ? Here is how : https://learn.microsoft.com/en-US/xamarin/xamarin-forms/user-interface/collectionview/layout – Nk54 Apr 18 '20 at 12:02
  • @Nk54 yes, and I'm trying to make it in XAML, not C# – haierophanto Apr 18 '20 at 12:03

1 Answers1

1

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.

Nk54
  • 751
  • 7
  • 15