4using System.Collections.ObjectModel;
5using System.Collections.Specialized;
7using System.Reflection;
8using Microsoft.Maui.Controls;
38 public static readonly BindableProperty OriginalItemsSourceProperty =
39 BindableProperty.Create(
40 nameof(OriginalItemsSource),
44 propertyChanged: OnOriginalItemsSourceChanged);
46 public IEnumerable OriginalItemsSource
48 get => (IEnumerable)this.GetValue(OriginalItemsSourceProperty);
49 set => this.SetValue(OriginalItemsSourceProperty, value);
53 public static readonly BindableProperty FilterProperty =
54 BindableProperty.Create(
56 typeof(Predicate<object>),
58 default(Predicate<object>),
59 propertyChanged: OnFilterChanged);
61 public Predicate<object> Filter
63 get => (Predicate<object>)this.GetValue(FilterProperty);
64 set => this.SetValue(FilterProperty, value);
68 public static readonly BindableProperty SortComparisonProperty =
69 BindableProperty.Create(
70 nameof(SortComparison),
71 typeof(Comparison<object>),
73 default(Comparison<object>),
74 propertyChanged: OnSortComparisonChanged);
76 public Comparison<object> SortComparison
78 get => (Comparison<object>)this.GetValue(SortComparisonProperty);
79 set => this.SetValue(SortComparisonProperty, value);
83 public static readonly BindableProperty SortPropertyNameProperty =
84 BindableProperty.Create(
85 nameof(SortPropertyName),
89 propertyChanged: OnSortPropertyNameChanged);
91 public string SortPropertyName
93 get => (string)this.GetValue(SortPropertyNameProperty);
94 set => this.SetValue(SortPropertyNameProperty, value);
98 public static readonly BindableProperty SortDirectionProperty =
99 BindableProperty.Create(
104 propertyChanged: OnSortDirectionChanged);
109 set => this.SetValue(SortDirectionProperty, value);
113 private static void OnOriginalItemsSourceChanged(BindableObject bindable,
object oldValue,
object newValue)
118 if (oldValue is INotifyCollectionChanged OldNotify)
120 OldNotify.CollectionChanged -= Control.OriginalItemsSource_CollectionChanged;
124 if (newValue is INotifyCollectionChanged NewNotify)
126 NewNotify.CollectionChanged += Control.OriginalItemsSource_CollectionChanged;
129 Control.ApplyFilterAndSort();
133 private void OriginalItemsSource_CollectionChanged(
object? sender, NotifyCollectionChangedEventArgs e)
135 this.ApplyFilterAndSort();
139 private static void OnFilterChanged(BindableObject bindable,
object oldValue,
object newValue)
142 Control.ApplyFilterAndSort();
146 private static void OnSortComparisonChanged(BindableObject bindable,
object oldValue,
object newValue)
149 Control.ApplyFilterAndSort();
153 private static void OnSortPropertyNameChanged(BindableObject bindable,
object oldValue,
object newValue)
156 Control.ApplyFilterAndSort();
160 private static void OnSortDirectionChanged(BindableObject bindable,
object oldValue,
object newValue)
163 Control.ApplyFilterAndSort();
169 private void ApplyFilterAndSort()
171 if (this.OriginalItemsSource is
null)
173 base.ItemsSource =
null;
178 IEnumerable<object> Items = this.OriginalItemsSource.Cast<
object>();
181 if (this.Filter is not
null)
183 Items = Items.Where(item => this.Filter(item));
188 if (this.SortComparison is not
null)
190 Items = Items.OrderBy(item => item, Comparer<object>.Create(this.SortComparison));
192 else if (!
string.IsNullOrWhiteSpace(this.SortPropertyName))
196 Items = Items.OrderBy(item => GetPropertyValue(item, this.SortPropertyName));
200 Items = Items.OrderByDescending(item => GetPropertyValue(item, this.SortPropertyName));
205 ObservableCollection<object> TransformedCollection =
new ObservableCollection<object>(Items);
208 base.ItemsSource = TransformedCollection;
214 private static object? GetPropertyValue(
object obj,
string propertyName)
216 if (obj is
null ||
string.IsNullOrWhiteSpace(propertyName))
219 PropertyInfo? Prop = obj.GetType().GetProperty(propertyName);
220 return Prop?.GetValue(obj,
null);
A CollectionView that supports dynamic filtering and sorting of its items.
SortDirection
Represents the direction in which sorting is performed.