Dynamically show/hide Header or Footer of Xamarin.Forms.ListView

后端 未结 7 1713
粉色の甜心
粉色の甜心 2021-02-09 18:29

Is there a way to dynamically show/hide the header of a ListView based on a condition at runtime.



        
7条回答
  •  野性不改
    2021-02-09 18:31

    You should use FooterTemplate and Footer properties. It works as ItemTemplate and ItemSource:

    
        
            
                
            
        
    
    

    And bind to Footer property something nullable (object for example). Or you can use converter to convert: true -> new object() and false -> null

    Also it is possible to create a subclass of ListView. My example (IsLoading property is what you searching for):

    Xaml:

    
    
        
            
                
                    
                        
                            
                        
                    
                            
            
        
    
    

    Code-behind:

    public partial class LoadableListView : ListView
    {
        public LoadableListView()
        {
            InitializeComponent();
    
            this.ItemAppearing += OnItemAppearing;
        }
    
        public static readonly BindableProperty IsLoadingProperty = BindableProperty.Create(
            nameof(IsLoading),
            typeof(bool),
            typeof(LoadableListView),
            false,
            propertyChanged: (bindable, oldValue, newValue) => 
            {
                var element = (LoadableListView)bindable;
    
                element.Footer = (bool)newValue ? new object() : null;
            }
        );
        public bool IsLoading
        {
            set => SetValue(IsLoadingProperty, value);
            get => (bool)GetValue(IsLoadingProperty);
        }
    
        public static readonly BindableProperty ScrolledDownCommandProperty = BindableProperty.Create(
            nameof(ScrolledDownCommand),
            typeof(ICommand),
            typeof(LoadableListView)
        );
        public ICommand ScrolledDownCommand
        {
            set => SetValue(ScrolledDownCommandProperty, value);
            get => (ICommand)GetValue(ScrolledDownCommandProperty);
        }
    
        void OnItemAppearing(object sender, ItemVisibilityEventArgs e)
        {
            if (ItemsSource == null) return;
            if (ScrolledDownCommand == null) return;
    
            object last = null;
    
            if (ItemsSource is IList)
            {
                var length = (ItemsSource as IList).Count;
                last = (ItemsSource as IList)[length - 1];
            }
            else 
            {
                foreach (var item in ItemsSource)
                    last = item;
            }
    
            if (e.Item == last && ScrolledDownCommand.CanExecute(null))
                ScrolledDownCommand.Execute(null);
    
        }
    

    Consuming:

     
                    
                        
                            
                        
                    
                
    

提交回复
热议问题