Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement an ItemTemplate within a ListView using XAML

How do I implement an ItemTemplate within a ListView using XAML?

If I do not use ItemTemplate, then my binding works and I receive no errors. However, I want to format my list view. As a result, I am attempting to use ItemTemplate but continue to hit a runtime exception:

Xamarin.Forms.Xaml.XamlParseException: Position 30:30. Cannot assign property "View": type mismatch between "Xamarin.Forms.TextCell" and "X…

I receive a runtime exception with the following XAML:

<ListView Grid.Row="1" Grid.Column="0" ItemsSource="{Binding Services}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <ViewCell.View>
                    <TextCell Text="{Binding Name}" Detail="{Binding Description}" />
                </ViewCell.View>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Any suggestions?

like image 356
Scott Nimrod Avatar asked Dec 13 '25 11:12

Scott Nimrod


1 Answers

  • ViewCell is a class which you can use to create custom cells layouts
  • TextCell is a predefined cell with predefined layout.

You cannot use a Cell inside another Cell (eg. TextCell in a ViewCell).

You can create a custom cell like this:

<ListView Grid.Row="1" Grid.Column="0" ItemsSource="{Binding Services}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <ViewCell.View>
                    <StackLayout Orientation="Vertical">
                        <Label Text="{Binding Name}" />
                        <Label Text="{Binding Description}" />
                    </StackLayout>                
                </ViewCell.View>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

OR use a predefined one:

<ListView Grid.Row="1" Grid.Column="0" ItemsSource="{Binding Services}">
    <ListView.ItemTemplate>
        <DataTemplate>
          <TextCell Text="{Binding Name}" Detail="{Binding Description}" />
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

A list of predefined cells is here: http://developer.xamarin.com/guides/cross-platform/xamarin-forms/controls/cells/

like image 126
Daniel Luberda Avatar answered Dec 16 '25 23:12

Daniel Luberda



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!