forked from jdeksup/VirtualizingObservableCollection
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
118 additions
and
43 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
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,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; } | ||
} | ||
} |
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
35 changes: 35 additions & 0 deletions
35
DataGridAsyncDemoMVVM/converters/DataGridSortingEventArgsToMemberPathSortDirection.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,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}; | ||
} | ||
} | ||
} |
15 changes: 4 additions & 11 deletions
15
...hangedEventArgsToDatagridHeaderAndText.cs → ...extChangedEventArgsToMemberPathAndText.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
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
10 changes: 10 additions & 0 deletions
10
DataGridAsyncDemoMVVM/filtersort/MemberPathSortingDirection.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,10 @@ | ||
using System.ComponentModel; | ||
|
||
namespace DataGridAsyncDemoMVVM.filtersort | ||
{ | ||
public class MemberPathSortingDirection | ||
{ | ||
public string MemberPath { get; set; } | ||
public ListSortDirection? SortDirection { 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