I\'m using StackLayout and ListView to show some part of a view, but the ListView takes more space than I need, and leaves a blank space between the last row of the list and
As hvaughan3 said it's really not a good practice. But I had the same case and I used workaround with behavior:
public class ListViewHeightBehavior : Behavior
{
private ListView _listView;
public static readonly BindableProperty ExtraSpaceProperty =
BindableProperty.Create(nameof(ExtraSpace),
typeof(double),
typeof(ListViewHeightBehavior),
0d);
public double ExtraSpace
{
get { return (double)GetValue(ExtraSpaceProperty); }
set { SetValue(ExtraSpaceProperty, value); }
}
protected override void OnAttachedTo(ListView bindable)
{
base.OnAttachedTo(bindable);
_listView = bindable;
_listView.PropertyChanged += (s, args) =>
{
var count = _listView.ItemsSource?.Count();
if (args.PropertyName == nameof(_listView.ItemsSource)
&& count.HasValue
&& count.Value > 0)
{
_listView.HeightRequest = _listView.RowHeight * count.Value + ExtraSpace;
}
};
}
}
XAML:
// Your template