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.
Hey Guy
Try this
<ListView x:Name="lstContatos" HasUnevenRows="True"> <ListView.ItemTemplate> <DataTemplate> <ViewCell> <ViewCell.View> <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> </ViewCell.View> </ViewCell> </DataTemplate> </ListView.ItemTemplate> </ListView>
Answers
Hey Guy
Try this
@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?
@RafaelMoura thanks. It worked.
thanks
Great !! It helped me a lot
thanks