Same like this functionality . but instead of Entry . i have image button , Once i click the image button picker will display . In that picker i have 3 option like All, First Option , Second Option. When the page load filter option will be "ALL".
Once i selected First option,list view will be filter based on the first option .
Same like for second option too ..
But without masterDetailpage
Thanks in Advance ,
Vasanth
Do you want to achieve the result like following gif?
If so , please add the following nuget packages firstly.
Refractored.MvvmHelpers
Then, here is my layout.
<StackLayout> <ImageButton Source="myIcon.png" Command="{Binding TextChangeCommand}"></ImageButton> <ListView ItemsSource="{Binding PhonesList}" IsGroupingEnabled="True" GroupDisplayBinding="{Binding Name}"> <ListView.ItemTemplate> <DataTemplate> <TextCell Text="{Binding Title}" /> </DataTemplate> </ListView.ItemTemplate> </ListView> </StackLayout>
Here is layout background code.
public partial class MainPage : ContentPage { public MainPage() { InitializeComponent(); this.BindingContext = new MyViewModel(Navigation); } }
Here is myviewModel.
using MvvmHelpers; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Diagnostics; using System.Linq; using System.Text; using System.Windows.Input; using Xamarin.Forms; namespace ListviewBinderPicker { public class MyViewModel { public ObservableRangeCollection<PhoneGroup> PhonesList { get; set; } = new ObservableRangeCollection<PhoneGroup>(); public ObservableRangeCollection<PhoneGroup> AllPhonesList { get; set; } = new ObservableRangeCollection<PhoneGroup>(); public ICommand TextChangeCommand { protected set; get; } public MyViewModel(Xamarin.Forms.INavigation navigation) { TextChangeCommand = new Command<PhoneGroup>(async (key) => { string action = await Application.Current.MainPage.DisplayActionSheet("What is your band", "Cancel", null, "Apple", "Huawei", "Samsung","All"); if ("Apple".Equals(action)) { PhonesList.Replace(new PhoneGroup("Apple", new[]{ new Phone { Title = "iPhone 6s", Price = 50000 }, new Phone { Title = "iPhone 7", Price = 38000 }})); // PhonesList.ReplaceRange(PhonesList.Where(a => a.Name == "Apple" )); } else if ("Huawei".Equals(action)) { // PhonesList = new ObservableRangeCollection<PhoneGroup>(); PhonesList.Replace(new PhoneGroup("Huawei", new[]{ new Phone { Title = "Huawei P10", Price = 10000 }, new Phone { Title = "Huawei Mate 8", Price = 29000 }})); } else if ("Samsung".Equals(action)) { // PhonesList = new ObservableRangeCollection<PhoneGroup>(); PhonesList.Replace(new PhoneGroup("Samsung", new[]{ new Phone { Title = "Galaxy S8", Price = 60000 }, new Phone { Title = "Galaxy S7 Edge", Price = 50000 }})); } else { PhonesList.Clear(); foreach (var item in AllPhonesList) { PhonesList.Add(item); } } Debug.WriteLine("Action: " + action); }); PhonesList.Add(new PhoneGroup("Apple", new[]{ new Phone { Title = "iPhone 6s", Price = 50000 }, new Phone { Title = "iPhone 7", Price = 38000 }})); PhonesList.Add(new PhoneGroup("Huawei", new[]{ new Phone { Title = "Huawei P10", Price = 10000 }, new Phone { Title = "Huawei Mate 8", Price = 29000 }})); PhonesList.Add(new PhoneGroup("Samsung", new[]{ new Phone { Title = "Galaxy S8", Price = 60000 }, new Phone { Title = "Galaxy S7 Edge", Price = 50000 }})); foreach (var item in PhonesList) { AllPhonesList.Add(item); } } } }
Here is my Model PhoneGroup.cs
.
using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Text; namespace ListviewBinderPicker { public class PhoneGroup : ObservableCollection<Phone> { public string Name { get; private set; } public PhoneGroup(string name) : base() { Name = name; } public PhoneGroup(string name, IEnumerable<Phone> source) : base(source) { Name = name; } } public class Phone { public string Title { get; set; } public int Price { get; set; } } }
Answers
Looks like this
I tried this but i couldn't achieve the result .
Can you guys try this link and let me know
https://montemagno.com/xamarin-forms-filtering-a-listview-with-a-bindable-picker/
Do you want to achieve the result like following gif?
If so , please add the following nuget packages firstly.
Refractored.MvvmHelpers
Then, here is my layout.
Here is layout background code.
Here is myviewModel.
Here is my Model
PhoneGroup.cs
.@LeonLu Do you this project (Solution) . If it Kindly send me
Please see the message.