From dfd5a5f3de5e5fa15524631ebce2242e2e6a53b6 Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Wed, 20 Mar 2019 21:43:34 +0100 Subject: [PATCH] Add FilterableGridViewColumn. --- ILSpy/Controls/FilterableGridViewColumn.cs | 112 +++++++++++++++++++++ ILSpy/ILSpy.csproj | 1 + ILSpy/themes/generic.xaml | 41 +++++++- 3 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 ILSpy/Controls/FilterableGridViewColumn.cs diff --git a/ILSpy/Controls/FilterableGridViewColumn.cs b/ILSpy/Controls/FilterableGridViewColumn.cs new file mode 100644 index 000000000..598fa6417 --- /dev/null +++ b/ILSpy/Controls/FilterableGridViewColumn.cs @@ -0,0 +1,112 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Input; +using System.Windows.Media; + +namespace ICSharpCode.ILSpy.Controls +{ + class FilterableGridViewColumn : SortableGridViewColumn + { + static readonly ComponentResourceKey headerTemplateKey = new ComponentResourceKey(typeof(FilterableGridViewColumn), "ColumnHeaderTemplate"); + + + public FilterableGridViewColumn() + { + this.SetValueToExtension(HeaderTemplateProperty, new DynamicResourceExtension(headerTemplateKey)); + } + + string filterBy; + + public string FilterBy { + get { return filterBy; } + set { + if (filterBy != value) { + filterBy = value; + OnPropertyChanged(new PropertyChangedEventArgs("FilterBy")); + } + } + } + + string filterFormatString; + + public string FilterFormatString { + get { return filterFormatString; } + set { + if (filterFormatString != value) { + filterFormatString = value; + OnPropertyChanged(new PropertyChangedEventArgs("FilterFormatString")); + } + } + } + + public static ListView GetParentView(DependencyObject obj) + { + return (ListView)obj.GetValue(ParentViewProperty); + } + + public static void SetParentView(DependencyObject obj, ListView value) + { + obj.SetValue(ParentViewProperty, value); + } + + public static readonly DependencyProperty ParentViewProperty = + DependencyProperty.RegisterAttached("ParentView", typeof(ListView), typeof(FilterableGridViewColumn), new PropertyMetadata(null)); + + public string FilterExpression { + get { return (string)GetValue(FilterExpressionProperty); } + set { SetValue(FilterExpressionProperty, value); } + } + + public static readonly DependencyProperty FilterExpressionProperty = + DependencyProperty.Register("FilterExpression", typeof(string), typeof(FilterableGridViewColumn), new PropertyMetadata(null, OnFilterExpressionChanged)); + + private static void OnFilterExpressionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + var header = d as FilterableGridViewColumn; + var view = GetParentView(header); + if (header == null || view == null) + return; + header.Filter(view, e.NewValue as string); + } + + void Filter(ListView grid, string filterExpression) + { + ColumnSortDirection currentDirection = GetSortDirection(grid); + ICollectionView dataView = CollectionViewSource.GetDefaultView(grid.ItemsSource); + + if (dataView == null) return; + + string filterBy = FilterBy; + if (filterBy == null) { + Binding binding = DisplayMemberBinding as Binding; + if (binding != null && binding.Path != null) { + filterBy = binding.Path.Path; + } + } + + dataView.Filter = delegate (object item) { + if (filterBy == null) + return true; + var pInfo = item.GetType().GetProperty(filterBy); + if (pInfo == null) + return false; + return Matches(filterExpression, string.Format("{0:" + FilterFormatString + "}", pInfo.GetValue(item))); + }; + + dataView.Refresh(); + } + + bool Matches(string filterExpression, string value) + { + return value?.Contains(filterExpression) == true; + } + } +} diff --git a/ILSpy/ILSpy.csproj b/ILSpy/ILSpy.csproj index 9f80a9b1c..924d0a615 100644 --- a/ILSpy/ILSpy.csproj +++ b/ILSpy/ILSpy.csproj @@ -116,6 +116,7 @@ Form + diff --git a/ILSpy/themes/generic.xaml b/ILSpy/themes/generic.xaml index a72a988d5..567ba81e3 100644 --- a/ILSpy/themes/generic.xaml +++ b/ILSpy/themes/generic.xaml @@ -10,7 +10,7 @@ Displays an up arrow or down arrow in the column header when the grid is sorted using that column. --> - + @@ -26,6 +26,45 @@ Data = "M 5,5 L 10,10 L 15,5 L 5,5"/> + + + + + + + + + + + + + + + + + + + + + + + + + + + +