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 the profile continuation. It seems my ListView has more lines than the real list's length, or it has a fixed Height, I don't know... Here's my XAML:
<ScrollView>
<StackLayout Orientation="Vertical" Spacing="10">
<Label Text="{Binding Establishment.Name}" TextColor="Black" HorizontalOptions="StartAndExpand" Style="{DynamicResource TitleStyle}" FontAttributes="Bold"/>
<Label Text="{Binding Establishment.Category.Name, StringFormat='Categoria: {0}'}" TextColor="Black" HorizontalOptions="StartAndExpand" FontAttributes="Bold"/>
<Label Text="{Binding Establishment.Description}" TextColor="Black" HorizontalOptions="StartAndExpand" LineBreakMode="WordWrap" XAlign="Start"/>
<Label Text="Contatos" TextColor="Black" HorizontalOptions="StartAndExpand" FontAttributes="Bold"/>
<ListView ItemsSource="{Binding Establishment.Contacts}" VerticalOptions="Start" HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding Value}" Detail="{Binding StringType}" DetailColor="Gray"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Label Text="Endereço" TextColor="Black" HorizontalOptions="StartAndExpand" FontAttributes="Bold"/>
<Label Text="{Binding Establishment.FullAddress}" TextColor="Black" HorizontalOptions="StartAndExpand" />
<Button Text="Ver no mapa"/>
</StackLayout>
</ScrollView>
How can I fix it? I saw people using HasUnevenRows="True"
but it didn't work to me.
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<ListView>
{
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:
<ListView ItemsSource="{Binding Items}"
HasUnevenRows="True">
<ListView.Behaviors>
<behaviors:ListViewHeightBehavior ExtraSpace="180"/>
</ListView.Behaviors>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
// Your template
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With