Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set ListView Row Height in Xamarin.Forms

I want to set height of the ListView in XAML file for iOS Device using below mentioned code and bindings.

  1. I've used observable collection to notify when the property value is updated and it is getting notified and sets the correct value but it is not being reflected in iOS Simulator or device but that works fine with Android & Windows Phone Devices.
<ListView  Grid.Row="2" Grid.Column="1"  ItemsSource="{Binding QuestionAnswerList,Mode=TwoWay}"  SelectedItem="{Binding SelectedData,Mode=TwoWay}"                   
        HasUnevenRows="true" 
        RowHeight="{Binding Custom_height,Mode=TwoWay}"  
        BackgroundColor="Silver" 
        HorizontalOptions="FillAndExpand" 
        VerticalOptions="FillAndExpand">
        <ListView.ItemTemplate>
  1. I've tried to set Row Height of List View statically as well but that also doesn't work.

  2. I haven't given any height and set Grid RowDefinition height = "*" but then also it doesn't set the height of the ListView instead it overlaps the details on the next ListView row.

Thanks.

like image 646
Nirav Mehta Avatar asked Sep 16 '14 13:09

Nirav Mehta


2 Answers

Try using a StackLayout instead of a ListView:

<StackLayout BindableLayout.ItemsSource="{Binding Items}">
    <BindableLayout.ItemTemplate>
        <DataTemplate>
            <StackLayout Margin="0,0,0,5">
                <Label Text="{Binding Name}"/>
            </StackLayout>
        </DataTemplate>
    </BindableLayout.ItemTemplate>
</StackLayout>
like image 93
Mulepower Avatar answered Oct 23 '22 23:10

Mulepower


On iOS, when you set ListView.HasUnevenRows to true, you also have to set the Cell.Height for each cell property too, as the renderer can not infer it from the content.

You can also use ListView.RowHeight like you do in your example, for even rows. In that case, do not set ListView.HasUnevenRows (or set it to false).

like image 31
Stephane Delcroix Avatar answered Oct 24 '22 00:10

Stephane Delcroix