Drake
Drake

Reputation: 2703

ListView not displaying labels correctly

In my Xamarin.Forms app, I have a ListView:

<ListView ItemsSource="{Binding MyItems}"
                  Grid.Row="1"
                  Margin="0,20,0,0">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Label Text="ABC" FontSize="Large" TextColor="Black" BackgroundColor="Red" Margin="20" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand"/>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
</ListView>

Where the "ABC" should appear, is only a thin red line for the background of the label. I don't see the actually letters. I've made many ListView before with no problem, I don't know what I'm doing wrong here?

Upvotes: 1

Views: 447

Answers (1)

Tom
Tom

Reputation: 1749

ViewCell needs to contain a layout control, otherwise it will not render correctly.

For simplicity, you could implement a StackLayout:

<ListView 
    ItemsSource="{Binding MyItems}"
    Grid.Row="1"
    Margin="0,20,0,0>
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout 
                    HorizontalOptions="FillAndExpand"
                    Margin="0"
                    Padding="0"
                    Spacing="0"
                    VerticalOptions="Fill">
                    <Label 
                        BackgroundColor="Red"
                        FontSize="Large"
                        HorizontalOptions="FillAndExpand"
                        HorizontalTextAlignment="Center"
                        Margin="20" 
                        Text="ABC" 
                        TextColor="Black"  
                        VerticalOptions="FillAndExpand"
                        VerticalTextAlignment="Center"/>
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Upvotes: 3

Related Questions