Adjust height of listview based on its content inside grid in xamarin forms

Viewed 569

I'm having grid layout in a popup with three rows.(Search, Listview and a button) I need to adjust the height of the listview based on the number of items inside listview.

Here is the layout page:

<pages:PopupPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:pages="clr-namespace:Rg.Plugins.Popup.Pages;assembly=Rg.Plugins.Popup"
             xmlns:animations="clr-namespace:Rg.Plugins.Popup.Animations;assembly=Rg.Plugins.Popup"
         x:Class="TCRMobile.Pages.Popups.CustomerContactsListPopup">
<pages:PopupPage.Animation>
    <animations:ScaleAnimation 
  PositionIn="Center"
  PositionOut="Center"
  ScaleIn="1.2"
  ScaleOut="0.8"
  DurationIn="100"
  DurationOut="100"
  EasingIn="SinIn"
  EasingOut="SinOut"
  HasBackgroundAnimation="True"/>
</pages:PopupPage.Animation>
<pages:PopupPage.Content>
    <StackLayout VerticalOptions="Center" HorizontalOptions="FillAndExpand" Margin="20" Padding="20, 20, 20, 20" BackgroundColor="White" Orientation="Vertical">
        <Label Text="Select Name" HorizontalOptions="StartAndExpand" FontSize="20" TextColor="Black"></Label>
        <Grid VerticalOptions="FillAndExpand">
            <Grid.RowDefinitions>
                <RowDefinition Height="1*"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition Height="1*"></RowDefinition>
            </Grid.RowDefinitions>
            <SearchBar Grid.Row="0" HorizontalOptions="FillAndExpand"  Placeholder="Search" PlaceholderColor="Gray" 
                       x:Name="searchItem" TextChanged="searchItem_TextChanged"></SearchBar>
            <ListView Grid.Row="1" x:Name="lvContacts" CachingStrategy="RecycleElement" HasUnevenRows="True" SeparatorVisibility="None"
                  ItemTapped="lvContacts_ItemTapped">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <Grid Margin="5">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"></RowDefinition>
                                </Grid.RowDefinitions>
                                <Label Grid.Row="0" Text="{Binding Name}" TextColor="Black" HorizontalOptions="FillAndExpand" VerticalOptions="CenterAndExpand" ></Label>
                            </Grid>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

                <Button Grid.Row="2" Text="Enter Name" TextColor="White" BackgroundColor="#8BC34A" Margin="80,0,80,0" Command="{Binding EnterNameButtonCommand}" ></Button>
        </Grid>

    </StackLayout>

My .cs file code is as follows:

 VM = new CustomerContactsListViewModel(this);
        this.BindingContext = VM;
        lvContacts.ItemsSource = VM.customerContacts;
        int i = VM.customerContacts.Count;
        int heightrowList = 40;
        i = (VM.customerContacts.Count * heightrowList);
        lvContacts.HeightRequest = i;

The above code is working differently with different heightrowList value.

Any help?

0 Answers
Related