Xamarin.Forms: ListView inside StackLayout: How to set height?
xamarin.forms
Solution
The solution in my case was to put the `ListView` inside a `StackLayout` and then put that `StackLayout` inside the main `StackLayout`. Then I could set the `VerticalOptions = LayoutOptions.FillAndExpand` on the inner `StackLayout` (the one containing the `ListView`) with the result that the `ListView` got the space it needed (which of course varies depending on the data).
Here is the main code:
listView.ItemsSource = alternativeCells;
listView.ItemSelected += ListViewOnItemSelected;
var listStackLayout = new StackLayout
{
VerticalOptions = LayoutOptions.FillAndExpand,
Orientation = StackOrientation.Vertical
};
listStackLayout.Children.Add(listView);
_stackLayout.Children.Add(listStackLayout);
As you see, I added a new `StackLayout` with the only purpose of putting the `ListView` inside it. Then I put that `listStackLayout` inside the main `_stackLayout`.
See the post on this Xamarin forum post for more information
Problem
In a `ContentPage` I have a `ListView` inside a `StackLayout` inside a `ScrollView`. The `ListView` is populated (`ItemSource` is set) in the `ContentPage` when `OnAppearing` gets called and I can see that the list is populated in the emulator. The `StackLayout`s orientation is `Vertical` and below the `ListView` I have a `Button`. My problem is that no matter how many elements the `ListView` has, it gets the height of 53.33. I would like the height of the `ListView` to match the total height of the items in it. By setting `HeightRequest` I can set the height of the `ListView` to anything I want, but since I do not know the height of the items inside the `ListView` the result is most often that the distance to the button below it is incorrect and therefore looks ugly. I have tried to set `VerticalOptions` on both the `ListView` and the `StackLayout` to `Start`and other settings, but this does not change the height from 53.33 (and if I try to combine using `HeightRequest` and `Start` it turns out that HeightRequest wins out). How can I solve this? (please excuse the cross posting from Xamarin forum)