Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Multi-select ListView

$
0
0

I am definitely new to xamarin but I had enough time to check it's possibilities and it looks great.

But there is one point where I had some problems. Multiselect doesn't look easy to run, so I found xamarin docs information how I can use it.
//developer.xamarin.com/recipes/cross-platform/xamarin-forms/controls/multiselect/

And the current code example:

    SelectMultipleBasePage<CheckItem> multiPage;

    async void OnClick(object sender, EventArgs ea)
    {
        var items = new List<CheckItem>();
        items.Add(new CheckItem { Title = "Xamarin.com"});
        items.Add(new CheckItem { Title = "Twitter"});
        items.Add(new CheckItem { Title = "Facebook"});
        items.Add(new CheckItem { Title = "Internet ad"});
        items.Add(new CheckItem { Title = "Online article"});
        items.Add(new CheckItem { Title = "Magazine ad"});
        items.Add(new CheckItem { Title = "Friends"});
        items.Add(new CheckItem { Title = "At work"});

        if (multiPage == null)
            multiPage = new SelectMultipleBasePage<CheckItem>(items) { Title = "" };

        await Navigation.PushAsync(multiPage);
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();

        if (multiPage != null)
        {
            results.Text = "";
            var answers = multiPage.GetSelection();
            foreach (var a in answers)
            {
                results.Text += a.Title + "; ";
            }
        }
        else
        {
            results.Text = "(none)";
        }
    }

    internal class CheckItem
    {
        public string Title { get; set; }
    }

    public class SelectMultipleBasePage<T> : ContentPage
    {
        public class WrappedSelection<T> : INotifyPropertyChanged
        {
            public T Item { get; set; }
            bool isSelected = false;
            public bool IsSelected
            {
                get
                {
                    return isSelected;
                }
                set
                {
                    if (isSelected != value)
                    {
                        isSelected = value;
                        PropertyChanged(this, new PropertyChangedEventArgs("IsSelected"));
                        //                      PropertyChanged (this, new PropertyChangedEventArgs (nameof (IsSelected))); // C# 6
                    }
                }
            }
            public event PropertyChangedEventHandler PropertyChanged = delegate { };
        }
        public class WrappedItemSelectionTemplate : ViewCell
        {
            public WrappedItemSelectionTemplate() : base()
            {
                Label Title = new Label();                   
                Title.SetBinding(Label.TextProperty, new Binding("Item.Title"));                 
                Switch mainSwitch = new Switch();
                mainSwitch.SetBinding(Switch.IsToggledProperty, new Binding("IsSelected"));
                RelativeLayout layout = new RelativeLayout();
                layout.Children.Add(Title,
                    Constraint.Constant(5),
                    Constraint.Constant(5),
                    Constraint.RelativeToParent(p => p.Width - 60),
                    Constraint.RelativeToParent(p => p.Height - 10)
                );

                layout.Children.Add(mainSwitch,
                    Constraint.RelativeToParent(p => p.Width - 55),
                    Constraint.Constant(5),
                    Constraint.Constant(50),
                    Constraint.RelativeToParent(p => p.Height - 10)
                );
                View = layout;
            }
        }
        public List<WrappedSelection<T>> WrappedItems = new List<WrappedSelection<T>>();
        public SelectMultipleBasePage(List<T> items)
        {
            WrappedItems = items.Select(item => new WrappedSelection<T>() { Item = item, IsSelected = false }).ToList();
            ListView mainList = new ListView()
            {
                ItemsSource = WrappedItems,
                ItemTemplate = new DataTemplate(typeof(WrappedItemSelectionTemplate)),
            };

            mainList.ItemSelected += (sender, e) =>
            {
                if (e.SelectedItem == null) return;
                var o = (WrappedSelection<T>)e.SelectedItem;
                o.IsSelected = !o.IsSelected;
                ((ListView)sender).SelectedItem = null; //de-select
            };
            Content = mainList;
            if (Device.RuntimePlatform == Device.Android)
            {   // fix issue where rows are badly sized (as tall as the screen) on WinPhone8.1
                mainList.RowHeight = 40;
                // also need icons for Windows app bar (other platforms can just use text)
                ToolbarItems.Add(new ToolbarItem("All", "check.png", SelectAll, ToolbarItemOrder.Primary));
                ToolbarItems.Add(new ToolbarItem("None", "cancel.png", SelectNone, ToolbarItemOrder.Primary));
            }
            else
            {
                ToolbarItems.Add(new ToolbarItem("Все", null, SelectAll, ToolbarItemOrder.Primary));
                ToolbarItems.Add(new ToolbarItem("Пусто", null, SelectNone, ToolbarItemOrder.Primary));
            }
        }
        void SelectAll()
        {
            foreach (var wi in WrappedItems)
            {
                wi.IsSelected = true;
            }
        }
        void SelectNone()
        {
            foreach (var wi in WrappedItems)
            {
                wi.IsSelected = false;
            }
        }
        public List<T> GetSelection()
        {
            return WrappedItems.Where(item => item.IsSelected).Select(wrappedItem => wrappedItem.Item).ToList();
        }
    }

But I'm stuck on trying to change the current data selection ("Xamarin.com","Twitter", "Facebook"...) to the data from my model.

In the case with picker it has Itemsource property to connect with my view model. But with case of Multiselect I'm completely stucked.

I beg you to give a code example, where this multiselect list will receive data from model.


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>