Skip to content

Commit

Permalink
Add sorting.
Browse files Browse the repository at this point in the history
  • Loading branch information
pluskal committed Dec 17, 2017
1 parent 210fdba commit 034fa51
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 43 deletions.
4 changes: 3 additions & 1 deletion DataGridAsyncDemoMVVM/DataGridAsyncDemoMVVM.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="converters\DataGridSortingEventArgsToMemberPathSortDirection.cs" />
<Compile Include="filtersort\MemberPathSortingDirection.cs" />
<Compile Include="filtersort\MemberPathFilterText.cs" />
<Compile Include="filtersort\DescriptionList.cs" />
<Compile Include="filtersort\FilterDescription.cs" />
Expand All @@ -85,7 +87,7 @@
<Compile Include="RemoteOrDbDataItem.cs" />
<Compile Include="RemoteOrDbDataSourceAsyncProxy.cs" />
<Compile Include="RemoteOrDbDataSourceEmulation.cs" />
<Compile Include="converters\TextChangedEventArgsToDatagridHeaderAndText.cs" />
<Compile Include="converters\TextChangedEventArgsToMemberPathAndText.cs" />
<Page Include="MainWindow.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
Expand Down
70 changes: 49 additions & 21 deletions DataGridAsyncDemoMVVM/MainViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,54 +1,82 @@
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.ComponentModel;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Data;
using AlphaChiTech.Virtualization;
using DataGridAsyncDemoMVVM.filtersort;
using GalaSoft.MvvmLight.Command;
using SortDescription = DataGridAsyncDemoMVVM.filtersort.SortDescription;

namespace DataGridAsyncDemoMVVM
{
internal class MainViewModel
{
private readonly VirtualizingObservableCollection<RemoteOrDbDataItem>
_myDataVirtualizedAsyncFilterSortObservableCollection;

private readonly RemoteOrDbDataSourceAsyncProxy _myRemoteOrDbDataSourceAsyncProxy;
private VirtualizingObservableCollection<RemoteOrDbDataItem> myDataVirtualizedAsyncFilterSortObservableCollection;

private int _filterWaitingCount;

public MainViewModel()
{
this._myRemoteOrDbDataSourceAsyncProxy = new RemoteOrDbDataSourceAsyncProxy(new RemoteOrDbDataSourceEmulation(100));
this.myDataVirtualizedAsyncFilterSortObservableCollection =
this._myRemoteOrDbDataSourceAsyncProxy =
new RemoteOrDbDataSourceAsyncProxy(new RemoteOrDbDataSourceEmulation(100));
this._myDataVirtualizedAsyncFilterSortObservableCollection =
new VirtualizingObservableCollection<RemoteOrDbDataItem>(
new PaginationManager<RemoteOrDbDataItem>(this._myRemoteOrDbDataSourceAsyncProxy,
pageSize: 10, maxPages: 2));
this.MyDataVirtualizedAsyncFilterSortObservableCollectionCollectionView =
CollectionViewSource.GetDefaultView(myDataVirtualizedAsyncFilterSortObservableCollection);
CollectionViewSource.GetDefaultView(this._myDataVirtualizedAsyncFilterSortObservableCollection);

this.FilterCommand = new RelayCommand<object>(async o => await this.Filter(o as MemberPathFilterText));
this.FilterCommand = new RelayCommand<MemberPathFilterText>(async o => await this.Filter(o));
this.SortCommand = new RelayCommand<MemberPathSortingDirection>(async o => await this.Sort(o));
}

private int _filterWaitingCount = 0;
private async Task Filter(MemberPathFilterText memberPathFilterText)
public ICollectionView MyDataVirtualizedAsyncFilterSortObservableCollectionCollectionView { get; }

public RelayCommand<MemberPathFilterText> FilterCommand { get; }

public RelayCommand<MemberPathSortingDirection> SortCommand { get; }

private async Task Sort(MemberPathSortingDirection memberPathSortingDirection)
{
if (String.IsNullOrWhiteSpace(memberPathFilterText.FilterText))
while (this._filterWaitingCount != 0)
await Task.Delay(500);
var sortDirection = memberPathSortingDirection.SortDirection;
var sortMemberPath = memberPathSortingDirection.MemberPath;
switch (sortDirection)
{
this._myRemoteOrDbDataSourceAsyncProxy.FilterDescriptionList.Remove(memberPathFilterText
.ColumnSortMemberPath);
case null:
this._myRemoteOrDbDataSourceAsyncProxy.SortDescriptionList.Remove(sortMemberPath);
break;
case ListSortDirection.Ascending:
this._myRemoteOrDbDataSourceAsyncProxy.SortDescriptionList.Add(
new SortDescription(sortMemberPath, ListSortDirection.Ascending));
break;
case ListSortDirection.Descending:
this._myRemoteOrDbDataSourceAsyncProxy.SortDescriptionList.Add(
new SortDescription(sortMemberPath, ListSortDirection.Descending));
break;
}

this._myRemoteOrDbDataSourceAsyncProxy.FilterDescriptionList.OnCollectionReset();
this._myDataVirtualizedAsyncFilterSortObservableCollection.Clear();
}

private async Task Filter(MemberPathFilterText memberPathFilterText)
{
if (string.IsNullOrWhiteSpace(memberPathFilterText.FilterText))
this._myRemoteOrDbDataSourceAsyncProxy.FilterDescriptionList.Remove(memberPathFilterText
.MemberPath);
else
{
this._myRemoteOrDbDataSourceAsyncProxy.FilterDescriptionList.Add(new FilterDescription(memberPathFilterText.ColumnSortMemberPath, memberPathFilterText.FilterText));
}
this._myRemoteOrDbDataSourceAsyncProxy.FilterDescriptionList.Add(
new FilterDescription(memberPathFilterText.MemberPath, memberPathFilterText.FilterText));
Interlocked.Increment(ref this._filterWaitingCount);
await Task.Delay(500);
if (Interlocked.Decrement(ref this._filterWaitingCount) != 0) return;
this._myRemoteOrDbDataSourceAsyncProxy.FilterDescriptionList.OnCollectionReset();
this.myDataVirtualizedAsyncFilterSortObservableCollection.Clear();
this._myDataVirtualizedAsyncFilterSortObservableCollection.Clear();
}

public ICollectionView MyDataVirtualizedAsyncFilterSortObservableCollectionCollectionView { get; }

public RelayCommand<object> FilterCommand { get; }
}
}
17 changes: 13 additions & 4 deletions DataGridAsyncDemoMVVM/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
Height="350"
DataContext="{StaticResource MainViewModel}">
<Window.Resources>
<converters:TextChangedEventArgsToDatagridHeaderAndText x:Key="TextChangedEventArgsToDatagridHeaderAndText" />
<converters:TextChangedEventArgsToMemberPathAndText x:Key="TextChangedEventArgsToMemberPathAndText" />
<converters:DataGridSortingEventArgsToMemberPathSortDirection
x:Key="DataGridSortingEventArgsToMemberPathSortDirection" />
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
Expand All @@ -22,7 +24,14 @@
Grid.Row="1"
CanUserSortColumns="True"
ItemsSource="{Binding MyDataVirtualizedAsyncFilterSortObservableCollectionCollectionView}">

<i:Interaction.Triggers>
<i:EventTrigger EventName="Sorting">
<cmd:EventToCommand
Command="{Binding Mode=OneWay, Path=SortCommand}"
EventArgsConverter="{StaticResource DataGridSortingEventArgsToMemberPathSortDirection}"
PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
<DataGrid.ColumnHeaderStyle>
<Style TargetType="DataGridColumnHeader">
<Setter Property="HorizontalAlignment" Value="Stretch" />
Expand Down Expand Up @@ -50,8 +59,8 @@
<cmd:EventToCommand
Command="{Binding Mode=OneWay,
Path=DataContext.FilterCommand,
RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
EventArgsConverter="{StaticResource TextChangedEventArgsToDatagridHeaderAndText}"
RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
EventArgsConverter="{StaticResource TextChangedEventArgsToMemberPathAndText}"
PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
Expand Down
2 changes: 0 additions & 2 deletions DataGridAsyncDemoMVVM/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#region

using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
using AlphaChiTech.Virtualization;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.ComponentModel;
using System.Windows.Controls;
using DataGridAsyncDemoMVVM.filtersort;
using GalaSoft.MvvmLight.Command;

namespace DataGridAsyncDemoMVVM.converters
{
public class DataGridSortingEventArgsToMemberPathSortDirection : IEventArgsConverter
{
public object Convert(object value, object parameter)
{
if (!(value is DataGridSortingEventArgs sortingEventArgs)) return null;

var sortMemberPath = sortingEventArgs.Column.SortMemberPath;
var sortDirection = sortingEventArgs.Column.SortDirection;

switch (sortDirection)
{
case null:
sortDirection = ListSortDirection.Ascending;
break;
case ListSortDirection.Ascending:
sortDirection = ListSortDirection.Descending;
break;
case ListSortDirection.Descending:
sortDirection = null;
break;
}
sortingEventArgs.Column.SortDirection = sortDirection;
sortingEventArgs.Handled = true;

return new MemberPathSortingDirection {MemberPath = sortMemberPath, SortDirection = sortDirection};
}
}
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,25 @@
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using DataGridAsyncDemoMVVM.filtersort;
using GalaSoft.MvvmLight.Command;

namespace DataGridAsyncDemoMVVM.converters
{
public class TextChangedEventArgsToDatagridHeaderAndText : IEventArgsConverter
public class TextChangedEventArgsToMemberPathAndText : IEventArgsConverter
{
public object Convert(object value, object parameter)
{
if (value is TextChangedEventArgs eventArgs)
{
if (eventArgs.Source is TextBox textBox)
{

DataGridColumnHeader dataGridColumnHeader = textBox.ParentOfType<DataGridColumnHeader>();
var dataGridColumnHeader = textBox.ParentOfType<DataGridColumnHeader>();
if (dataGridColumnHeader != null)
{
string columnSortMemberPath = dataGridColumnHeader.Column.SortMemberPath;
var columnSortMemberPath = dataGridColumnHeader.Column.SortMemberPath;
var filterText = textBox.Text;
return new MemberPathFilterText{ ColumnSortMemberPath =columnSortMemberPath, FilterText= filterText };
return new MemberPathFilterText {MemberPath = columnSortMemberPath, FilterText = filterText};
}
}
}
return null;
}
}
Expand Down
2 changes: 1 addition & 1 deletion DataGridAsyncDemoMVVM/filtersort/MemberPathFilterText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace DataGridAsyncDemoMVVM.filtersort
{
public class MemberPathFilterText
{
public string ColumnSortMemberPath { get; set; }
public string MemberPath { get; set; }
public string FilterText { get; set; }
}
}
10 changes: 10 additions & 0 deletions DataGridAsyncDemoMVVM/filtersort/MemberPathSortingDirection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.ComponentModel;

namespace DataGridAsyncDemoMVVM.filtersort
{
public class MemberPathSortingDirection
{
public string MemberPath { get; set; }
public ListSortDirection? SortDirection { get; set; }
}
}
5 changes: 2 additions & 3 deletions DataGridAsyncDemoMVVM/filtersort/ParentOfTypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public static void TestNotNull(this object parameter, string parameterName)
public static void TestNotEmptyString(this string parameter, string parameterName)
{
if (string.IsNullOrEmpty(parameter))
throw new ArgumentException($@"The parameter '{parameterName}' should not be empty string.", parameterName);
throw new ArgumentException($@"The parameter '{parameterName}' should not be empty string.",
parameterName);
}
}

Expand Down Expand Up @@ -97,10 +98,8 @@ private static DependencyObject GetParent(this DependencyObject element)
{
var parent = VisualTreeHelper.GetParent(element);
if (parent == null)
{
if (element is FrameworkElement frameworkElement)
parent = frameworkElement.Parent;
}
return parent;
}
}
Expand Down
1 change: 1 addition & 0 deletions DataGridAsyncDemoMVVM/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>

<packages>
<package id="CommonServiceLocator" version="1.3" targetFramework="net47" />
<package id="MvvmLightLibs" version="5.3.0.0" targetFramework="net47" />
Expand Down

0 comments on commit 034fa51

Please sign in to comment.