-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Move ListView sort logic to behavior class
Moved ListView sorting logic from code-behind to a new behavior class `ListViewClickSortBehavior`.
- Loading branch information
1 parent
cb051e5
commit a0eccec
Showing
5 changed files
with
91 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using System.Windows.Controls.Primitives; | ||
using FoliCon.Modules.utils; | ||
|
||
namespace FoliCon.Modules.UI; | ||
|
||
public class ListViewClickSortBehavior : Behavior<ListView> | ||
{ | ||
private GridViewColumnHeader _lastHeaderClicked; | ||
private ListSortDirection _lastDirection = ListSortDirection.Ascending; | ||
|
||
protected override void OnAttached() | ||
{ | ||
base.OnAttached(); | ||
|
||
AssociatedObject.AddHandler(ButtonBase.ClickEvent, new RoutedEventHandler(OnClick)); | ||
((INotifyCollectionChanged) AssociatedObject.Items).CollectionChanged += ListView_CollectionChanged; | ||
} | ||
|
||
protected override void OnDetaching() | ||
{ | ||
base.OnDetaching(); | ||
|
||
AssociatedObject.RemoveHandler(ButtonBase.ClickEvent, new RoutedEventHandler(OnClick)); | ||
((INotifyCollectionChanged) AssociatedObject.Items).CollectionChanged -= ListView_CollectionChanged; | ||
} | ||
|
||
private void ListView_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) | ||
{ | ||
if (e.Action == NotifyCollectionChangedAction.Reset) | ||
{ | ||
UiUtils.SetColumnWidth(AssociatedObject); | ||
} | ||
} | ||
|
||
private void OnClick(object sender, RoutedEventArgs e) | ||
{ | ||
if (e.OriginalSource is not GridViewColumnHeader headerClicked) return; | ||
if (headerClicked.Role == GridViewColumnHeaderRole.Padding) return; | ||
|
||
ListSortDirection direction; | ||
if (headerClicked != _lastHeaderClicked) | ||
{ | ||
direction = ListSortDirection.Ascending; | ||
} | ||
else | ||
{ | ||
direction = _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(AssociatedObject.ItemsSource); | ||
dataView.SortDescriptions.Clear(); | ||
var sd = new SortDescription(sortBy, direction); | ||
dataView.SortDescriptions.Add(sd); | ||
dataView.Refresh(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,17 @@ | ||
using FoliCon.Modules.utils; | ||
|
||
namespace FoliCon.Views; | ||
namespace FoliCon.Views; | ||
|
||
/// <summary> | ||
/// Interaction logic for SearchResult | ||
/// </summary> | ||
public partial class SearchResult | ||
{ | ||
private GridViewColumnHeader _lastHeaderClicked; | ||
private ListSortDirection _lastDirection = ListSortDirection.Ascending; | ||
|
||
public SearchResult() | ||
{ | ||
InitializeComponent(); | ||
((INotifyCollectionChanged)ListViewResult.Items).CollectionChanged += ListView_CollectionChanged; | ||
} | ||
|
||
private void ListView_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) | ||
{ | ||
if (e.Action == NotifyCollectionChangedAction.Reset) | ||
{ | ||
UiUtils.SetColumnWidth(ListViewResult); | ||
} | ||
} | ||
|
||
private void ListViewResult_OnClick(object sender, RoutedEventArgs e) | ||
private void UserControl_Unloaded(object sender, RoutedEventArgs e) | ||
{ | ||
if (e.OriginalSource is not GridViewColumnHeader headerClicked) | ||
{ | ||
return; | ||
} | ||
|
||
if (headerClicked.Role == GridViewColumnHeaderRole.Padding) | ||
{ | ||
return; | ||
} | ||
|
||
ListSortDirection direction; | ||
if (headerClicked != _lastHeaderClicked) | ||
{ | ||
direction = ListSortDirection.Ascending; | ||
} | ||
else | ||
{ | ||
direction = _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; | ||
WebBox.Browser.Dispose(); | ||
} | ||
|
||
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(); | ||
} | ||
|
||
private void UserControl_Unloaded(object sender, RoutedEventArgs e) | ||
{ | ||
WebBox.Browser.Dispose(); | ||
} | ||
} |