Skip to content

Commit

Permalink
Sorting enabled in searchResult poster mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
DineshSolanki committed Aug 10, 2021
1 parent 4783467 commit d848440
Show file tree
Hide file tree
Showing 7 changed files with 224 additions and 16 deletions.
3 changes: 2 additions & 1 deletion FoliCon/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
xmlns:langs="clr-namespace:FoliCon.Properties.Langs">
<Application.Resources>
<ResourceDictionary>
<langs:LangProvider x:Key="FoliConLangs"/>
<ResourceDictionary.MergedDictionaries>
<hc:ThemeResources UsingSystemTheme="True"/>
<ResourceDictionary Source="pack://application:,,,/HandyControl;component/Themes/Basic/Colors/Light.xaml"/>
<ResourceDictionary Source="pack://application:,,,/HandyControl;component/Themes/Theme.xaml"/>
<ResourceDictionary Source="pack://application:,,,/FoliCon;component/XamlResources/UiElements.xaml"/>
</ResourceDictionary.MergedDictionaries>
<langs:LangProvider x:Key="FoliConLangs"/>
</ResourceDictionary>
</Application.Resources>
</prism:PrismApplication>
131 changes: 131 additions & 0 deletions FoliCon/Modules/ListViewGridSortableBehavior.cs
Original file line number Diff line number Diff line change
@@ -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:<GridViewColumn DisplayMemberBinding="{Binding Year}" Width="60">
// <GridViewColumn.Header>
// <GridViewColumnHeader modules:ListViewGridSortableBehavior.SortHeaderString="Year">
// <StackPanel Orientation="Horizontal">
// <Label Content="↓" Visibility="{Binding Path=SortDownVisibility, RelativeSource={RelativeSource AncestorType={x:Type GridViewColumnHeader}}}"></Label>
// <Label Content="↑" Visibility="{Binding Path=SortUpVisibility, RelativeSource={RelativeSource AncestorType={x:Type GridViewColumnHeader}}}"></Label>
// <Label Content="Author"></Label>
// </StackPanel>
// </GridViewColumnHeader>
// </GridViewColumn.Header>
// </GridViewColumn>

namespace FoliCon.Modules
{
public class ListViewGridSortableBehavior : Behavior<ListView>
{
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);
}
}
}
15 changes: 10 additions & 5 deletions FoliCon/ViewModels/SearchResultViewModel.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -111,7 +115,7 @@ public bool IsSearchFocused
#region Commands

public DelegateCommand PickCommand { get; }
public DelegateCommand SortResultCommand { get; }
public DelegateCommand<RoutedEventArgs> SortResultCommand => new(SortResult);
public DelegateCommand SkipCommand { get; }
public DelegateCommand SkipAllCommand { get; }
public DelegateCommand SearchAgainCommand { get; }
Expand All @@ -125,16 +129,17 @@ 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;
CloseDialog("false");
});
}

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)
Expand Down
5 changes: 3 additions & 2 deletions FoliCon/Views/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
<MenuItem x:Name="MenuApiConfigBtn" Header="{ex:Lang Key={x:Static langs:LangKeys.APIKeysConfiguration}}"
Command="{Binding ApiConfigCommand}" />
<Separator />
<hc:ComboBox x:Name="CmbLanguage" AutoComplete="True" ItemsSource="{Binding Source={hc:EnumBindingSource {x:Type models:Languages}}}" IsEditable="False" hc:Poptip.Content="{ex:Lang Key={x:Static langs:LangKeys.ChangeLanguage}}" hc:Poptip.Placement="BottomRight" SelectedItem ="{Binding Path=AppLanguage}" SelectionChanged="CmbLanguage_OnSelectionChanged"/>
<hc:ComboBox x:Name="CmbLanguage" AutoComplete="True" ItemsSource="{Binding Source={hc:EnumBindingSource {x:Type models:Languages}}}" IsEditable="False" hc:Poptip.Content="{ex:Lang Key={x:Static langs:LangKeys.ChangeLanguage}}" hc:Poptip.Placement="BottomRight" SelectedItem ="{Binding Path=AppLanguage}" SelectionChanged="CmbLanguage_OnSelectionChanged"/>
</MenuItem>
<MenuItem x:Name="MenuRExbtn" Header="{ex:Lang Key={x:Static langs:LangKeys.RestartExplorer}}"
ToolTip="{ex:Lang Key={x:Static langs:LangKeys.RestartExplorerTooltip}}"
Expand Down Expand Up @@ -109,7 +109,8 @@
</i:EventTrigger>
</i:Interaction.Triggers>
<TextBlock Text="{Binding SelectedFolder}" HorizontalAlignment="Center" VerticalAlignment="Top"
FontStyle="Italic" Foreground="Black"/>
FontStyle="Italic" Foreground="Black">
</TextBlock>
</ContentControl>

<Grid Grid.Column="0" Grid.Row="2" Grid.IsSharedSizeScope="True" Background="WhiteSmoke">
Expand Down
20 changes: 12 additions & 8 deletions FoliCon/Views/SearchResult.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }">

<prism:Dialog.WindowStyle>
<Style TargetType="Window">
<Setter Property="Height" Value="514.5" />
Expand Down Expand Up @@ -50,7 +51,10 @@
<Button Content="{ex:Lang Key={x:Static langs:LangKeys.Skip}}" HorizontalAlignment="Right" VerticalAlignment="Top" Grid.Row="0" Style="{StaticResource ButtonDanger}"
Command="{Binding SkipCommand}" IsCancel="True" ToolTip="{ex:Lang Key={x:Static langs:LangKeys.SkipThisTitle}}" />
<ListView x:Name="ListViewResult" ItemsSource="{Binding ResultListViewData.Data}"
SelectedItem="{Binding ResultListViewData.SelectedItem}" Grid.Row="1" hc:Empty.ShowEmpty="true">
SelectedItem="{Binding ResultListViewData.SelectedItem}" Grid.Row="1" hc:Empty.ShowEmpty="true" GridViewColumnHeader.Click="ListViewResult_OnClick">
<i:Interaction.Behaviors>
<modules:ListViewGridSortableBehavior/>
</i:Interaction.Behaviors>
<hc:Interaction.Triggers>
<!--<hc:EventTrigger EventName="GridViewColumnHeader.Click">
<hc:EventToCommand Command="{Binding SortResultCommand}" PassEventArgsToCommand="True"/>
Expand All @@ -61,16 +65,16 @@
</hc:Interaction.Triggers>
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Title}" Width="auto">
<GridViewColumn.CellTemplate>
<GridViewColumn DisplayMemberBinding="{Binding Title}" Width="auto" Header="{ex:Lang Key={x:Static langs:LangKeys.Title}}"/>
<!--<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Title}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
<GridViewColumnHeader Tag="Title" Content="{ex:Lang Key={x:Static langs:LangKeys.Title}}" />
</GridViewColumn>
<GridViewColumn Header="{ex:Lang Key={x:Static langs:LangKeys.Year}}" Width="60" DisplayMemberBinding="{Binding Year}" />
<GridViewColumn Header="{ex:Lang Key={x:Static langs:LangKeys.Rating}}" Width="60" DisplayMemberBinding="{Binding Rating}" />
</GridViewColumn.CellTemplate>-->
<!--<GridViewColumnHeader />-->

<GridViewColumn Header="{ex:Lang Key={x:Static langs:LangKeys.Year}}" Width="60" DisplayMemberBinding="{Binding Year}"/>
<GridViewColumn Header="{ex:Lang Key={x:Static langs:LangKeys.Rating}}" Width="60" DisplayMemberBinding="{Binding Rating}"/>
</GridView>
</ListView.View>
</ListView>
Expand Down
45 changes: 45 additions & 0 deletions FoliCon/Views/SearchResult.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using FoliCon.Modules;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace FoliCon.Views
{
Expand All @@ -8,6 +12,9 @@ namespace FoliCon.Views
/// </summary>
public partial class SearchResult
{
private GridViewColumnHeader _lastHeaderClicked;
private ListSortDirection _lastDirection = ListSortDirection.Ascending;

public SearchResult()
{
InitializeComponent();
Expand All @@ -23,5 +30,43 @@ private void ListView_CollectionChanged(object sender, NotifyCollectionChangedEv
// //ListViewResult.ScrollIntoView(e.NewItems[0]);
}
}

private void ListViewResult_OnClick(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;
var header = headerClicked.Column.Header as string;
Sort(header, direction);

headerClicked.Column.HeaderTemplate = direction == ListSortDirection.Ascending
? Application.Current.Resources["HeaderTemplateArrowUp"] as DataTemplate
: Application.Current.Resources["HeaderTemplateArrowDown"] as DataTemplate;

// 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(ListViewResult.ItemsSource);

dataView.SortDescriptions.Clear();
var sd = new SortDescription(sortBy, direction);
dataView.SortDescriptions.Add(sd);
dataView.Refresh();
}
}
}
21 changes: 21 additions & 0 deletions FoliCon/XamlResources/UiElements.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<DataTemplate x:Key="HeaderTemplateArrowUp">
<DockPanel>
<TextBlock HorizontalAlignment="Center" Text="{Binding}"/>
<Path x:Name="arrow"
StrokeThickness = "1"
Fill = "gray"
Data = "M 5,10 L 15,10 L 10,5 L 5,10"/>
</DockPanel>
</DataTemplate>
<DataTemplate x:Key="HeaderTemplateArrowDown">
<DockPanel>
<TextBlock HorizontalAlignment="Center" Text="{Binding }"/>
<Path x:Name="arrow"
StrokeThickness = "1"
Fill = "gray"
Data = "M 5,5 L 10,10 L 15,5 L 5,5"/>
</DockPanel>
</DataTemplate>
</ResourceDictionary>

0 comments on commit d848440

Please sign in to comment.