From d848440a5c0d59d6729e2e3ab99b66b72e52e485 Mon Sep 17 00:00:00 2001 From: Dinesh Solanki <15937452+DineshSolanki@users.noreply.github.com> Date: Tue, 10 Aug 2021 21:50:08 +0530 Subject: [PATCH] Sorting enabled in searchResult poster mode. --- FoliCon/App.xaml | 3 +- .../Modules/ListViewGridSortableBehavior.cs | 131 ++++++++++++++++++ FoliCon/ViewModels/SearchResultViewModel.cs | 15 +- FoliCon/Views/MainWindow.xaml | 5 +- FoliCon/Views/SearchResult.xaml | 20 +-- FoliCon/Views/SearchResult.xaml.cs | 45 ++++++ FoliCon/XamlResources/UiElements.xaml | 21 +++ 7 files changed, 224 insertions(+), 16 deletions(-) create mode 100644 FoliCon/Modules/ListViewGridSortableBehavior.cs create mode 100644 FoliCon/XamlResources/UiElements.xaml diff --git a/FoliCon/App.xaml b/FoliCon/App.xaml index b99b0bad..d071abea 100644 --- a/FoliCon/App.xaml +++ b/FoliCon/App.xaml @@ -5,12 +5,13 @@ xmlns:langs="clr-namespace:FoliCon.Properties.Langs"> + + - \ No newline at end of file diff --git a/FoliCon/Modules/ListViewGridSortableBehavior.cs b/FoliCon/Modules/ListViewGridSortableBehavior.cs new file mode 100644 index 00000000..a2245915 --- /dev/null +++ b/FoliCon/Modules/ListViewGridSortableBehavior.cs @@ -0,0 +1,131 @@ +using System.ComponentModel; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Controls.Primitives; +using System.Windows.Data; +using Microsoft.Xaml.Behaviors; + +//https://www.codewrecks.com/post/old/2014/04/sorting-a-wpf-listview-in-grid-mode/ +//Usage: +// +// +// +// +// +// +// +// +// +// + +namespace FoliCon.Modules +{ + public class ListViewGridSortableBehavior : Behavior + { + public static readonly DependencyProperty HeaderStringProperty = + DependencyProperty.Register(nameof(HeaderString), typeof(object), typeof(GridViewColumnHeader)); + public object HeaderString + { + get => GetValue(HeaderStringProperty); + set => SetValue(HeaderStringProperty, value); + } + protected override void OnAttached() + { + AssociatedObject?.AddHandler(ButtonBase.ClickEvent, new RoutedEventHandler(GridHeaderClickEventHandler)); + base.OnAttached(); + } + + private GridViewColumnHeader _lastHeaderClicked; + private ListSortDirection _lastDirection = ListSortDirection.Ascending; + void GridHeaderClickEventHandler(object sender, RoutedEventArgs e) + { + if (e.OriginalSource is not GridViewColumnHeader headerClicked) return; + if (headerClicked.Role == GridViewColumnHeaderRole.Padding) return; + var direction = headerClicked != _lastHeaderClicked + ? ListSortDirection.Ascending + : _lastDirection == ListSortDirection.Ascending ? ListSortDirection.Descending : ListSortDirection.Ascending; + if (_lastHeaderClicked != null) + { + SetSortDownVisibility(_lastHeaderClicked, Visibility.Collapsed); + SetSortUpVisibility(_lastHeaderClicked, Visibility.Collapsed); + } + //string header = headerClicked.Column.Header as string; + var sortString = GetSortHeaderString(headerClicked); + if (string.IsNullOrEmpty(sortString)) return; + Sort(sortString, direction); + if (direction == ListSortDirection.Ascending) + { + SetSortDownVisibility(headerClicked, Visibility.Collapsed); + SetSortUpVisibility(headerClicked, Visibility.Visible); + } + else + { + SetSortDownVisibility(headerClicked, Visibility.Visible); + SetSortUpVisibility(headerClicked, Visibility.Collapsed); + } + // Remove arrow from previously sorted header + if (_lastHeaderClicked != null && _lastHeaderClicked != headerClicked) + { + _lastHeaderClicked.Column.HeaderTemplate = null; + } + _lastHeaderClicked = headerClicked; + _lastDirection = direction; + } + private void Sort(string sortBy, ListSortDirection direction) + { + var dataView = + CollectionViewSource.GetDefaultView(AssociatedObject.ItemsSource); + dataView.SortDescriptions.Clear(); + var sd = new SortDescription(sortBy, direction); + dataView.SortDescriptions.Add(sd); + dataView.Refresh(); + } + public static readonly DependencyProperty SortHeaderStringProperty = + DependencyProperty.RegisterAttached + ( + "SortHeaderString", + typeof(string), + typeof(GridViewColumnHeader) + ); + public static string GetSortHeaderString(DependencyObject obj) + { + return (string)obj.GetValue(SortHeaderStringProperty); + } + public static void SetSortHeaderString(DependencyObject obj, string value) + { + obj.SetValue(SortHeaderStringProperty, value); + } + public static readonly DependencyProperty SortDownVisibilityProperty = + DependencyProperty.RegisterAttached + ( + "SortDownVisibility", + typeof(Visibility), + typeof(GridViewColumnHeader), + new UIPropertyMetadata(Visibility.Collapsed) + ); + public static Visibility GetSortDownVisibility(DependencyObject obj) + { + return (Visibility)obj.GetValue(SortDownVisibilityProperty); + } + public static void SetSortDownVisibility(DependencyObject obj, Visibility value) + { + obj.SetValue(SortDownVisibilityProperty, value); + } + public static readonly DependencyProperty SortUpVisibilityProperty = + DependencyProperty.RegisterAttached + ( + "SortUpVisibility", + typeof(Visibility), + typeof(GridViewColumnHeader), + new UIPropertyMetadata(Visibility.Collapsed) + ); + public static Visibility GetSortUpVisibility(DependencyObject obj) + { + return (Visibility)obj.GetValue(SortUpVisibilityProperty); + } + public static void SetSortUpVisibility(DependencyObject obj, Visibility value) + { + obj.SetValue(SortUpVisibilityProperty, value); + } + } +} diff --git a/FoliCon/ViewModels/SearchResultViewModel.cs b/FoliCon/ViewModels/SearchResultViewModel.cs index d4b4af0c..3a20d25e 100644 --- a/FoliCon/ViewModels/SearchResultViewModel.cs +++ b/FoliCon/ViewModels/SearchResultViewModel.cs @@ -1,15 +1,19 @@ using FoliCon.Models; using FoliCon.Modules; -using Prism.Commands; using Prism.Mvvm; using Prism.Services.Dialogs; using System; using System.Collections.Generic; using System.Globalization; +using System.Windows; +using System.Windows.Controls; using System.Windows.Input; using FoliCon.Properties.Langs; -using HandyControl.Controls; using HandyControl.Tools.Extension; +using DelegateCommand = Prism.Commands.DelegateCommand; +using MessageBox = HandyControl.Controls.MessageBox; +using HandyControl.Tools.Command; +using Prism.Commands; namespace FoliCon.ViewModels { @@ -111,7 +115,7 @@ public bool IsSearchFocused #region Commands public DelegateCommand PickCommand { get; } - public DelegateCommand SortResultCommand { get; } + public DelegateCommand SortResultCommand => new(SortResult); public DelegateCommand SkipCommand { get; } public DelegateCommand SkipAllCommand { get; } public DelegateCommand SearchAgainCommand { get; } @@ -125,7 +129,6 @@ public SearchResultViewModel(IDialogService dialogService) SkipCommand = new DelegateCommand(delegate { CloseDialog("false"); }); ResultListViewData = new ListViewData { Data = null, SelectedItem = null }; PickCommand = new DelegateCommand(PickMethod); - SortResultCommand = new DelegateCommand(SortResult); SkipAllCommand = new DelegateCommand(delegate { GlobalVariables.SkipAll = true; @@ -133,8 +136,10 @@ public SearchResultViewModel(IDialogService dialogService) }); } - private void SortResult() + private void SortResult(RoutedEventArgs e) { + string clickedHeader = (e.OriginalSource as GridViewColumnHeader)?.Column.Header.ToString(); + MessageBox.Show(clickedHeader); } protected virtual void CloseDialog(string parameter) diff --git a/FoliCon/Views/MainWindow.xaml b/FoliCon/Views/MainWindow.xaml index 923ecaf2..9f4a447c 100644 --- a/FoliCon/Views/MainWindow.xaml +++ b/FoliCon/Views/MainWindow.xaml @@ -70,7 +70,7 @@ - + + FontStyle="Italic" Foreground="Black"> + diff --git a/FoliCon/Views/SearchResult.xaml b/FoliCon/Views/SearchResult.xaml index cc01f6f3..3f54b9a0 100644 --- a/FoliCon/Views/SearchResult.xaml +++ b/FoliCon/Views/SearchResult.xaml @@ -13,6 +13,7 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" prism:ViewModelLocator.AutoWireViewModel="True" d:DataContext="{d:DesignInstance viewModels:SearchResultViewModel }"> +