-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a couple DataGrid grouping sorting and filtering examples for #8
- Loading branch information
Showing
21 changed files
with
754 additions
and
182 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,9 @@ | ||
<Application x:Class="DGGroupSortFilterExampleConcurrent.App" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:local="clr-namespace:DGGroupSortFilterExampleConcurrent" | ||
StartupUri="MainWindow.xaml"> | ||
<Application.Resources> | ||
|
||
</Application.Resources> | ||
</Application> |
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
File renamed without changes.
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
145 changes: 145 additions & 0 deletions
145
ExamplesAndTests/DGGroupSortFilterExampleConcurrent/Controls/CustomSortBehavior.cs
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,145 @@ | ||
// from https://stackoverflow.com/questions/18122751/wpf-datagrid-customsort-for-each-column/18218963 | ||
|
||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.ComponentModel; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Windows; | ||
using System.Windows.Controls; | ||
using System.Windows.Data; | ||
|
||
namespace DGGroupSortFilterExampleConcurrent.Controls | ||
{ | ||
/// <summary> | ||
/// Optional behavior for doing custom sorting | ||
/// </summary> | ||
/// <example> | ||
/// <Window.Resources> | ||
/// <local:CustomSorter x:Key="MySorter"/> | ||
/// </Window.Resources> | ||
/// | ||
/// <Grid> | ||
/// <DataGrid ItemsSource = "{Binding ...}" | ||
/// local:CustomSortBehavior.CustomSorter = | ||
/// local:CustomSortBehavior.AllowCustomSort="True" | ||
/// <DataGrid.Columns> | ||
/// <DataGridTextColumn Header = "Column 1" Binding="{Binding Column1}"/> | ||
/// <DataGridTextColumn Header = "Column 2" Binding="{Binding Column2}"/> | ||
/// <DataGridTextColumn Header = "Column 3" Binding="{Binding Column3}"/> | ||
/// </DataGrid.Columns> | ||
/// </DataGrid> | ||
/// </Grid> | ||
/// </Window> | ||
/// </example> | ||
public class CustomSortBehavior | ||
{ | ||
#region Fields and Constants | ||
|
||
public static readonly DependencyProperty CustomSorterProperty = | ||
DependencyProperty.RegisterAttached("CustomSorter", typeof(ICustomSorter), typeof(CustomSortBehavior)); | ||
|
||
public static readonly DependencyProperty AllowCustomSortProperty = | ||
DependencyProperty.RegisterAttached("AllowCustomSort", | ||
typeof(bool), | ||
typeof(CustomSortBehavior), | ||
new UIPropertyMetadata(false, OnAllowCustomSortChanged)); | ||
|
||
#endregion | ||
|
||
#region public Methods | ||
|
||
public static bool GetAllowCustomSort(DataGrid grid) | ||
{ | ||
return (bool)grid.GetValue(AllowCustomSortProperty); | ||
} | ||
|
||
|
||
public static ICustomSorter GetCustomSorter(DataGrid grid) | ||
{ | ||
return (ICustomSorter)grid.GetValue(CustomSorterProperty); | ||
} | ||
|
||
public static void SetAllowCustomSort(DataGrid grid, bool value) | ||
{ | ||
grid.SetValue(AllowCustomSortProperty, value); | ||
} | ||
|
||
|
||
public static void SetCustomSorter(DataGrid grid, ICustomSorter value) | ||
{ | ||
grid.SetValue(CustomSorterProperty, value); | ||
} | ||
|
||
#endregion | ||
|
||
#region nonpublic Methods | ||
|
||
private static void HandleCustomSorting(object sender, DataGridSortingEventArgs e) | ||
{ | ||
var dataGrid = sender as DataGrid; | ||
if (dataGrid == null || !GetAllowCustomSort(dataGrid)) | ||
{ | ||
return; | ||
} | ||
|
||
var listColView = dataGrid.ItemsSource as ListCollectionView; | ||
if (listColView == null) | ||
{ | ||
throw new Exception("The DataGrid's ItemsSource property must be of type, ListCollectionView"); | ||
} | ||
|
||
// Sanity check | ||
var sorter = GetCustomSorter(dataGrid); | ||
if (sorter == null) | ||
{ | ||
return; | ||
} | ||
|
||
// The guts. | ||
e.Handled = true; | ||
|
||
var direction = (e.Column.SortDirection != ListSortDirection.Ascending) | ||
? ListSortDirection.Ascending | ||
: ListSortDirection.Descending; | ||
|
||
e.Column.SortDirection = sorter.SortDirection = direction; | ||
sorter.SortMemberPath = e.Column.SortMemberPath; | ||
|
||
listColView.CustomSort = sorter; | ||
} | ||
|
||
private static void OnAllowCustomSortChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) | ||
{ | ||
var existing = d as DataGrid; | ||
if (existing == null) | ||
{ | ||
return; | ||
} | ||
|
||
var oldAllow = (bool)e.OldValue; | ||
var newAllow = (bool)e.NewValue; | ||
|
||
if (!oldAllow && newAllow) | ||
{ | ||
existing.Sorting += HandleCustomSorting; | ||
} | ||
else | ||
{ | ||
existing.Sorting -= HandleCustomSorting; | ||
} | ||
} | ||
|
||
#endregion | ||
} | ||
|
||
public interface ICustomSorter : IComparer | ||
{ | ||
ListSortDirection SortDirection { get; set; } | ||
|
||
string SortMemberPath { get; set; } | ||
} | ||
|
||
} |
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
68 changes: 68 additions & 0 deletions
68
ExamplesAndTests/DGGroupSortFilterExampleConcurrent/Controls/ProjectDetailsSorter.cs
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,68 @@ | ||
using DGGroupSortFilterExampleConcurrent.ViewModels; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace DGGroupSortFilterExampleConcurrent.Controls | ||
{ | ||
/// <summary> | ||
/// An optimised sorter for use with ProjectDetails objects | ||
/// </summary> | ||
public class ProjectDetailsSorter : ICustomSorter | ||
{ | ||
private Func<object, object, int> _comparer = (x, y) => 0; | ||
|
||
private void UpdateComparer() | ||
{ | ||
int upDown = SortDirection == ListSortDirection.Ascending ? 1 : -1; | ||
|
||
Func<ProjectDetails, ProjectDetails, int> selector = SortMemberPath switch | ||
{ | ||
nameof(ProjectDetails.ProjectName) => (x, y) => upDown * Comparer<string>.Default.Compare(x.ProjectName, y.ProjectName), | ||
nameof(ProjectDetails.Complete) => (x, y) => upDown * Comparer<bool>.Default.Compare(x.Complete, y.Complete), | ||
nameof(ProjectDetails.DueDate) => (x, y) => upDown * Comparer<DateTime>.Default.Compare(x.DueDate, y.DueDate), | ||
nameof(ProjectDetails.TaskName) => (x, y) => upDown * Comparer<string>.Default.Compare(x.TaskName, y.TaskName), | ||
_ => (x, y) => 0 | ||
}; | ||
|
||
_comparer = (a, b) => | ||
{ | ||
if (a is ProjectDetails x && b is ProjectDetails y) | ||
{ | ||
return selector(x, y); | ||
} | ||
else | ||
{ | ||
return 0; | ||
} | ||
}; | ||
} | ||
|
||
private ListSortDirection _listSortDirection = ListSortDirection.Ascending; | ||
public ListSortDirection SortDirection | ||
{ | ||
get => _listSortDirection; | ||
set | ||
{ | ||
_listSortDirection = value; | ||
UpdateComparer(); | ||
} | ||
} | ||
|
||
private string _sortMemberPath = string.Empty; | ||
public string SortMemberPath | ||
{ | ||
get => _sortMemberPath; | ||
set | ||
{ | ||
_sortMemberPath = value; | ||
UpdateComparer(); | ||
} | ||
} | ||
|
||
public int Compare(object x, object y) => _comparer(x, y); | ||
} | ||
} |
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
4 changes: 2 additions & 2 deletions
4
.../DGGroupSortFilterExample/MainWindow.xaml → ...rtFilterExampleConcurrent/MainWindow.xaml
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
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
18 changes: 9 additions & 9 deletions
18
...ndTests/DGGroupSortFilterExample/App.xaml → ...s/DGGroupSortFilterExampleNormal/App.xaml
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,9 +1,9 @@ | ||
<Application x:Class="EditableDataGridTest.App" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:local="clr-namespace:EditableDataGridTest" | ||
StartupUri="MainWindow.xaml"> | ||
<Application.Resources> | ||
|
||
</Application.Resources> | ||
</Application> | ||
<Application x:Class="DGGroupSortFilterExample.App" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:local="clr-namespace:DGGroupSortFilterExample" | ||
StartupUri="MainWindow.xaml"> | ||
<Application.Resources> | ||
|
||
</Application.Resources> | ||
</Application> |
Oops, something went wrong.