I like to bind a command to my list view so I don’t have to create a wrapper to pass the item tapped command. I also deactivate the separator which behave very strange sometimes depends on the device. I prefer to handle it myself. I have added a DesactivateSelection property so once you’ve tapped an item, the command is executed and the item gets unselected.

I created this control before the pull to refresh and pull to load event were added to the list view. I still not have clean this control yet. It’s in my todo ;D

//
// <copyright file=ExtListView.cs company=Krossapp>
//     Copyright (c) Krossapp. All rights reserved.
// </copyright>
// <author>Nicolas Krier</author>
//
namespace SDF.XForm.Controls
{
    #region Using

    using System.Diagnostics.CodeAnalysis;
    using System.Windows.Input;
    using Xamarin.Forms;

    #endregion

    /// <summary>
    /// List view.
    /// </summary>
    public class ExtListView : ListView
    {
        #region Bindable property

        /// <summary>
        /// The command refresh property.
        /// </summary>
        [SuppressMessage(StyleCop.CSharp.MaintainabilityRules, SA1401:FieldsMustBePrivate, Justification = Reviewed.)]
        public static BindableProperty CommandRefreshProperty = BindableProperty.Create<ExtListView, ICommand>(x => x.CommandRefresh, null);

        /// <summary>
        /// The deselection auto property.
        /// </summary>
        [SuppressMessage(StyleCop.CSharp.MaintainabilityRules, SA1401:FieldsMustBePrivate, Justification = Reviewed.)]
        public static BindableProperty DesactivateSelectionProperty = BindableProperty.Create<ExtListView, bool>(x => x.DesactivateSelection, false);

        /// <summary>
        /// The command clicked property.
        /// </summary>
        [SuppressMessage(StyleCop.CSharp.MaintainabilityRules, SA1401:FieldsMustBePrivate, Justification = Reviewed.)]
        public static BindableProperty CommandProperty = BindableProperty.Create<ExtListView, ICommand>(x => x.Command, null);

        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref=ExtListView/> class.
        /// </summary>
        public ExtListView()
        {
            this.SeparatorVisibility = SeparatorVisibility.None;

            // this.ItemTapped += this.OnItemTapped;
            this.ItemSelected += this.OnItemSelected;
        }

        #endregion

        #region Property

        /// <summary>
        /// Gets or sets a value indicating whether this <see cref=ListView/> deselection auto.
        /// </summary>
        /// <value><c>true</c> if deselection auto; otherwise, <c>false</c>.</value>
        public bool DesactivateSelection
        {
            get { return (bool)this.GetValue(DesactivateSelectionProperty); }
            set { this.SetValue(DesactivateSelectionProperty, value); }
        }

        /// <summary>
        /// Gets or sets the command clicked.
        /// </summary>
        /// <value>The command clicked.</value>
        public ICommand Command
        {
            get { return (ICommand)this.GetValue(CommandProperty); }
            set { this.SetValue(CommandProperty, value); }
        }

        /// <summary>
        /// Gets or sets the command refresh.
        /// </summary>
        /// <value>The command refresh.</value>
        public ICommand CommandRefresh
        {
            get { return (ICommand)GetValue(CommandRefreshProperty); }
            set { this.SetValue(CommandRefreshProperty, value); }
        }

        #endregion

        /// <summary>
        /// Raises the item tapped event.
        /// </summary>
        /// <param name=sender>The sender.</param>
        /// <param name=e>The event args.</param>
        private void OnItemTapped(object sender, ItemTappedEventArgs e)
        {
            if (e.Item != null && this.Command != null && this.Command.CanExecute(e))
            {
                this.Command.Execute(e.Item);
            }
        }

        /// <summary>
        /// Raises the item selected event.
        /// </summary>
        /// <param name=sender>The sender.</param>
        /// <param name=e>The event args.</param>
        private void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
        {
            // We force the selected item so the view model can handle differents events.
            this.SelectedItem = e.SelectedItem;

            if (e.SelectedItem != null && this.Command != null && this.Command.CanExecute(e))
            {
                this.Command.Execute(e.SelectedItem);
            }

            if (this.DesactivateSelection)
            {
                this.SelectedItem = null;
            }
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *