I have a pretty large form (adapted mainly for tablets), that has a TabbedPage
nesting a ScrollView
and a vertical StackPanel
containing many controls.
I have few occurrences where I have a ListView
that contains a few single-line items, and I need it to size to content.
I'd like to get rid of its scroll-bars, but anyway I don't want it to take up more space than what's required for its items.
Is there a way (even an ugly one) to achieve that without have to write a renderer x3 platforms?
Here's a pseudo describing my tree:
<ContentPage>
<MasterDetailPage>
<MasterDetailPage.Detail>
<TabbedPage>
<ContentPage>
<ScrollView>
<StackPanel>
<!-- many controls-->
<ListView>
When rendered, there is a huge gap coming after the ListView
. How can I avoid that?
I tried messing around with the VerticalOptions
and HeightRequest
, non of which worked.
I'm looking for a dynamic way (preferably without inheritance) to achieve that without involving custom renderers.
I could make a event handler that takes into account the on the changing size of the ListView cells. Here's it:
Frame can be changed by Grid, StackLayout, etc. xaml.cs:
When Frame is displaying (ListView.ItemTemplate is applying), size of frame changing. We take it's actual height via Measure() method and put it into Dictionary, which knows about current ListView and holds Frame's height. When last Frame is shown, we sum all heights. If there's no items, ListView_OnSizeChanged() sets listView.HeightRequest to 0.
I may be grossly oversimplifying things here, but just adding
HasUnevenRows="True"
to the ListView worked for me.Based on Lutaaya's answer, I made a behavior that automates this, determining and setting the row-height (Gist).
Behavior:
Usage:
Ok Assume your ListView is Populated with NewsFeeds, lets use an
ObservableCollection
to contain our data to populate a ListView as Below :XAML Code :
C# Code
Then you assign the trends List to the ListView :
Then , we have make some Logic on the ListView and the data , So that the ListView Wraps the data , as the data increases the ListView also increases and viceversa :
Therefore the complete code is :
Hope it Helps .