question

DimChris avatar image
0 Votes"
DimChris asked LeonLu-MSFT commented

Get each listview's row height

Hello and hope everyone doing well, i need to calculate each listview's row height. My listivew has uneven rows.
Or if it is possible to get all rows height by once. Thank you

dotnet-xamarin
· 6
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

if it is possible to get all rows height by once.

I am afraid this answer is no. If rows do not appear in the screen, row cannot be rendered, so rows' height we cannot get it. I create a demo, I get the View height in the <ViewCell Appearing="ViewCell_Appearing" >, But I find I cannot get height when Row appear at the first time, but I can get height at the second time.

0 Votes 0 ·

Thank you LeonLu. I tried view cell appearing but i am getting always height = -1.
Here is My code:

 <StackLayout>
     <Button Clicked="Button_Clicked"/>
     <ListView x:Name="test" HasUnevenRows="True">
         <ListView.ItemTemplate>
             <DataTemplate>
                 <ViewCell Appearing="ViewCell_Appearing">
                     <StackLayout >
                         <Label Text="{Binding Name}"/>
                     </StackLayout>
                 </ViewCell>
             </DataTemplate>
         </ListView.ItemTemplate>
     </ListView>
 </StackLayout>


  public class Customer
     {
         public string Name { get; set; }
     }
     private void Button_Clicked(object sender, EventArgs e)
     {
         var items = new ObservableCollection<Customer>();
         items.Add(new Customer { Name = "Dimitris" });

         test.ItemsSource = items;
     }
     private async void ViewCell_Appearing(object sender, EventArgs e)
     {
         var ss = sender as ViewCell;
         await DisplayAlert("", ss.Height.ToString(), "OK");
     }
0 Votes 0 ·

Or if it is possible to get each row height after appears in screen.
Example

 foreach (var row in listView)
 {

 }
0 Votes 0 ·

I set CachingStrategy="RecycleElement", then here is my onAppearing code. Please notice, when your row appear at the first time, the height value is -1, You can get the height value at the second appearing time,

private  void ViewCell_Appearing(object sender, EventArgs e)
        {
            var cell = sender as ViewCell;
            var myView=cell.View;
            var getHeight= myView.Height;
            Console.WriteLine("======"+ getHeight + "===========");     
        }
1 Vote 1 ·
Show more comments

0 Answers