There is more space than I need in ListView

前端 未结 5 1240
生来不讨喜
生来不讨喜 2021-01-19 19:56

I\'m using StackLayout and ListView to show some part of a view, but the ListView takes more space than I need, and leaves a blank space between the last row of the list and

5条回答
  •  自闭症患者
    2021-01-19 20:08

    As hvaughan3 said it's really not a good practice. But I had the same case and I used workaround with behavior:

    public class ListViewHeightBehavior : Behavior
    {
        private ListView _listView;
    
        public static readonly BindableProperty ExtraSpaceProperty =
            BindableProperty.Create(nameof(ExtraSpace),
                                    typeof(double),
                                    typeof(ListViewHeightBehavior),
                                    0d);
    
        public double ExtraSpace
        {
            get { return (double)GetValue(ExtraSpaceProperty); }
            set { SetValue(ExtraSpaceProperty, value); }
        }
    
        protected override void OnAttachedTo(ListView bindable)
        {
            base.OnAttachedTo(bindable);
    
            _listView = bindable;
            _listView.PropertyChanged += (s, args) =>
            {
                var count = _listView.ItemsSource?.Count();
                if (args.PropertyName == nameof(_listView.ItemsSource)
                        && count.HasValue
                        && count.Value > 0)
                {
                    _listView.HeightRequest = _listView.RowHeight * count.Value + ExtraSpace;
                }
            };
        }
    }
    

    XAML:

         
          
            
          
          
            
              
                 // Your template
              
            
          
        
    

提交回复
热议问题