Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using TextCell in a StackLayout in a ListView

Why can I not use a TextCell like this in a ListView item template? When I use it the rows render but they are empty.

    <ListView ItemsSource="{Binding Courses}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout Orientation="Vertical">
                            <TextCell Text="{Binding Title}" Detail="{Binding SubTitle}"></TextCell>
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

When using a Label I can see the text contents in each row:

    <ListView ItemsSource="{Binding Courses}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout Orientation="Vertical">
                        <Label Text="{Binding Title}"></Label>
                        <Label Text="{Binding SubTitle}"></Label>
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

Is there anyway I can use the TextCell inside the list item template? I am trying to build a more complext layout inside the StackLayout and it would be greatly simplified if I could re-use the Title/Detail structure of the TextCell.

like image 353
span Avatar asked Dec 07 '25 17:12

span


1 Answers

According to the Xamarin.Forms Cell Reference, cells are only designed to be added to ListViews or TableViews. In particular, it says:

However Cell is not a visual element, it just describes a template for creating a visual element.

So it cannot be added directly to the children of a StackLayout. You will have to create a ViewCell with a custom template for that.. You can probably look at the source code on Github to find out the proper spacing that a TextCell uses between it's Text and TextDetail labels, to keep it consistent.

like image 91
sme Avatar answered Dec 11 '25 02:12

sme



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!