How to create an inline data template in .NET MAUI?

Platform: .NET MAUI| Category: Templates

First, create a model class for assigning values in the data template. Then, create Collection with ItemSource and finally add the data template to visualize the content.

XAML

<CollectionView>
        <CollectionView.ItemsSource>
            <x:Array Type="{x:Type local:Person}">
                <local:Person Name="Steve" Age="21" Location="USA" />
                <local:Person Name="John" Age="37" Location="USA" />
                <local:Person Name="Tom" Age="42" Location="UK" />
                <local:Person Name="Lucas" Age="29" Location="Germany" />
                <local:Person Name="Tariq" Age="39" Location="UK" />
                <local:Person Name="Jane" Age="30" Location="USA" />
            </x:Array>
        </CollectionView.ItemsSource>
        <CollectionView.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Label Text="{Binding Name}" FontAttributes="Bold" />
                    <Label Grid.Column="1" Text="{Binding Age}" />
                    <Label Grid.Column="2" Text="{Binding Location}" />
                </Grid>
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.