user2185592
user2185592

Reputation: 388

Setting HeightRequest of ListView as sum of all the Item's RowHeight

Inherent behavior of ListView is it always renders with fixed height irrespective of items it is showing. If more items are there then ListView will automatically show scrollbar. Now I have ListView which has been put inside the ScrollViewer. As ListView is inside ScrollViewer, ListView's Scrollbar doesn't function (it doesn't even show up) hence I'm not able to see all the items in my Listview. So what I want is ListView height should be such that it show up all the items without any scrollbar. My searching on this issue suggests that issue is known issue without any proper fix. I tried various workaround as suggested on different posts however it creates one or another problem at runtime.

Only way to fix this problem seems to be adding up the Row heights of items dynamically upon loading of the list and set the sum of all the item's Rowheight to HeightRequest of ListView. So can anyone suggest proper way of calculating the RowHeight of each item. Pls note that I have uneven rows i.e. each item's rowheight can be different.

Looking forward to help pointers/code snippets.

Thanks

Upvotes: 1

Views: 652

Answers (1)

Patrick
Patrick

Reputation: 1737

I don't think you can get the row height of the items dynamically. However, you can define a height property in your view model (eg. ItemHeight in this code example) and bind the HeightRequest property of each cell in the ListView. Then you can simply loop through the items source of the ListView and get the sum of the height.

XAML listview:

<ListView HasUnevenRows="true">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <AbsoluteLayout HeightRequest="{Binding ItemHeight}">
                    ...
                </AbsoluteLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

C# view model:

public class ItemViewModel
{
    ...
    public double ItemHeight { get; set; } = 50;
}

You can assign different values to the ItemHeight for different items according to the content that you are showing in the ListView.

Upvotes: 2

Related Questions