Forum Xamarin.Forms

ListView Data Binding in XAML

I'm trying to bind a List<> to a ListView. This code was working on WinRT

PostListView.ItemsSource = postList;

<ListView x:Name="PostListView" Grid.Row="1">
  <ListView.ItemTemplate>
    <DataTemplate>

       <!--<TextCell Text="{Binding title}"  Detail="{Binding body}"/>-->
      <Grid>
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="1*"/>
          <ColumnDefinition Width="1*"/>
          <ColumnDefinition Width="2*"/>
          <ColumnDefinition Width="5*"/>
        </Grid.ColumnDefinitions>

        <Label Grid.Column="0" Text="{Binding userId}"></Label>
        <Label Grid.Column="1" Text="{Binding id}"></Label>
        <Label Grid.Column="2" Text="{Binding title}"></Label>
        <Label Grid.Column="3" Text="{Binding body}"></Label>

      </Grid>

    </DataTemplate>


  </ListView.ItemTemplate>
</ListView>

ERROR: Unable to cast object of type 'Xamarin.Forms.Grid' to type 'Xamarin.Forms.Cell'.

BUT, when I replace the whole Grid block with this, it works:

<TextCell Text="{Binding title}" Detail="{Binding body}"/>

I want to show 4 different values in a tabular form.

Best Answer

Answers

  • RafaelMouraRafaelMoura BRInsider, University, Developer Group Leader ✭✭✭

    @DushyantBangal
    Data Template need a ViewCell. The TextCell class extend ViewCell and Grid not. U need Create a ViewCell and put your Grid inside, understand?

  • DushyantBangalDushyantBangal USMember ✭✭

    @RafaelMoura thanks. It worked. :smile:

  • gokcemgokcem USMember

    thanks

  • 001lilou001lilou FRMember

    Great !! It helped me a lot
    thanks

Sign In or Register to comment.