Forum Xamarin Xamarin.Forms

ListView Row Height

AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

Hi guys.

I have a ListView with HasUnevenRows = true.

I bind ItemSource to an ObservableCollection.

This Collection fill the ListViews with some rows. Each row has a different height because the text is different from row to row.

I would like to know if I can know which is the Height of each row.

Thanks
Alessandro

                IEnumerable<PropertyInfo> pInfos = (ListViewPrivacy as ItemsView<Cell>).GetType().GetRuntimeProperties();
                var templatedItems = pInfos.FirstOrDefault(info => info.Name == "TemplatedItems");
                if (templatedItems != null)
                {
                    var cells = templatedItems.GetValue(ListViewPrivacy);
                    foreach (ViewCell cell in cells as Xamarin.Forms.ITemplatedItemsList<Xamarin.Forms.Cell>)
                    {

                        if (cell.BindingContext != null)
                        {

                            System.Diagnostics.Debug.WriteLine("CellHeight = " + cell.Height + " - " + cell.RenderHeight + " " + cell.View.Height + " - " + cell.View.HeightRequest + " - " + cell.View.MinimumHeightRequest + " - " );
                            Grid grid = (Grid)cell.View;
                            System.Diagnostics.Debug.WriteLine("Height = " +  grid.Height.ToString() + " - " + grid.HeightRequest + " - " + grid.MinimumHeightRequest);


                        }
                    }
                }

I have tried with a code like this but I have always "-1" in each property

Posts

  • Amar_BaitAmar_Bait DZMember ✭✭✭✭✭
    edited July 2019

    Maybe a custom ViewCell where you listen to size changes? Or you bind the Height property of the your ViewCell parent view to your item (View)Model?

    <ViewCell>
        <StackLayout Orientation="Horizontal" Height="{Binding RowHeight, Mode=OneWayToSource}">
            <Label Text="{Binding Name}" />
            <Image Source="{Binding PictureUrl}" />
        </StackLayout>
    </ViewCell>
    
    public class ItemViewModel : BindableObject
    {
        public string Name { get; set; }
        public string PictureUrl { get; set; }
        public double RowHeight { get ; set; }
    }
    

    Custom View cell method:

    public class CustomViewCell : ViewCell
        {
            protected override void OnPropertyChanged([CallerMemberName] string propertyName = null)
            {
                base.OnPropertyChanged(propertyName);
    
            // Or RenderHeight property, dunno which one gets the actual row height value
                if (propertyName == nameof(Height) && Height > 0) 
                {
                    // use Height (or RenderHeight) here....
                }
            }
        }
    
  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    Maybe. Do you have a piece of code for this?

  • Amar_BaitAmar_Bait DZMember ✭✭✭✭✭

    I edited my post, check again

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    Thanks @Amar_Bait I take a look tomorrow

Sign In or Register to comment.