Reputation: 2150
I have ListView and I would like that it occupies height for exactly 5 elements. If list has 2 items, there would be space left at the button. If list view has 8 items, user would need to scroll to view remaining 3 items. Is it possible in Xamarin Forms?
I tried to set HeightRequest, but I don't know height of items.
Upvotes: 0
Views: 417
Reputation: 1127
You can try to subclass ListView and set the Element renderheight to calculate the total listview height. Am adding some similar code here which might help you.
public MainPage()
{
InitializeComponent();
BindingContext = new MainViewModel();
BackgroundColor = Color.Silver;
userListView = new ClistView();
userListView.BackgroundColor = Color.Olive;
userListView.VerticalOptions = LayoutOptions.Start;
userListView.ItemsSource = (BindingContext as MainViewModel).Items;
userListView.SizeChanged += VisualElement_OnSizeChanged;
userListView.RowHeight = 80;
Content = userListView;
}
private void VisualElement_OnSizeChanged(object sender, EventArgs e)
{
if (userListView.HeightRequest < 0)
userListView.HeightRequest = userListView.ElementHeight * 5;
}
CListView - CustomListView
public class ClistView : ListView
{
public double ElementHeight { get; set; }
protected override void SetupContent(Cell content, int index)
{
if (ElementHeight <= 0 && RowHeight <= 0)
ElementHeight = content.RenderHeight;
else
ElementHeight = RowHeight;
base.SetupContent(content, index);
}
}
Upvotes: 1