1- using System ;
2- using System . ComponentModel ;
3- using System . Diagnostics ;
1+ using System . ComponentModel ;
42using System . Threading ;
53using System . Threading . Tasks ;
64using System . Windows . Data ;
75using AlphaChiTech . Virtualization ;
86using DataGridAsyncDemoMVVM . filtersort ;
97using GalaSoft . MvvmLight . Command ;
8+ using SortDescription = DataGridAsyncDemoMVVM . filtersort . SortDescription ;
109
1110namespace DataGridAsyncDemoMVVM
1211{
1312 internal class MainViewModel
1413 {
14+ private readonly VirtualizingObservableCollection < RemoteOrDbDataItem >
15+ _myDataVirtualizedAsyncFilterSortObservableCollection ;
16+
1517 private readonly RemoteOrDbDataSourceAsyncProxy _myRemoteOrDbDataSourceAsyncProxy ;
16- private VirtualizingObservableCollection < RemoteOrDbDataItem > myDataVirtualizedAsyncFilterSortObservableCollection ;
18+
19+ private int _filterWaitingCount ;
1720
1821 public MainViewModel ( )
1922 {
20- this . _myRemoteOrDbDataSourceAsyncProxy = new RemoteOrDbDataSourceAsyncProxy ( new RemoteOrDbDataSourceEmulation ( 100 ) ) ;
21- this . myDataVirtualizedAsyncFilterSortObservableCollection =
23+ this . _myRemoteOrDbDataSourceAsyncProxy =
24+ new RemoteOrDbDataSourceAsyncProxy ( new RemoteOrDbDataSourceEmulation ( 100 ) ) ;
25+ this . _myDataVirtualizedAsyncFilterSortObservableCollection =
2226 new VirtualizingObservableCollection < RemoteOrDbDataItem > (
2327 new PaginationManager < RemoteOrDbDataItem > ( this . _myRemoteOrDbDataSourceAsyncProxy ,
2428 pageSize : 10 , maxPages : 2 ) ) ;
2529 this . MyDataVirtualizedAsyncFilterSortObservableCollectionCollectionView =
26- CollectionViewSource . GetDefaultView ( myDataVirtualizedAsyncFilterSortObservableCollection ) ;
30+ CollectionViewSource . GetDefaultView ( this . _myDataVirtualizedAsyncFilterSortObservableCollection ) ;
2731
28- this . FilterCommand = new RelayCommand < object > ( async o => await this . Filter ( o as MemberPathFilterText ) ) ;
32+ this . FilterCommand = new RelayCommand < MemberPathFilterText > ( async o => await this . Filter ( o ) ) ;
33+ this . SortCommand = new RelayCommand < MemberPathSortingDirection > ( async o => await this . Sort ( o ) ) ;
2934 }
3035
31- private int _filterWaitingCount = 0 ;
32- private async Task Filter ( MemberPathFilterText memberPathFilterText )
36+ public ICollectionView MyDataVirtualizedAsyncFilterSortObservableCollectionCollectionView { get ; }
37+
38+ public RelayCommand < MemberPathFilterText > FilterCommand { get ; }
39+
40+ public RelayCommand < MemberPathSortingDirection > SortCommand { get ; }
41+
42+ private async Task Sort ( MemberPathSortingDirection memberPathSortingDirection )
3343 {
34- if ( String . IsNullOrWhiteSpace ( memberPathFilterText . FilterText ) )
44+ while ( this . _filterWaitingCount != 0 )
45+ await Task . Delay ( 500 ) ;
46+ var sortDirection = memberPathSortingDirection . SortDirection ;
47+ var sortMemberPath = memberPathSortingDirection . MemberPath ;
48+ switch ( sortDirection )
3549 {
36- this . _myRemoteOrDbDataSourceAsyncProxy . FilterDescriptionList . Remove ( memberPathFilterText
37- . ColumnSortMemberPath ) ;
50+ case null :
51+ this . _myRemoteOrDbDataSourceAsyncProxy . SortDescriptionList . Remove ( sortMemberPath ) ;
52+ break ;
53+ case ListSortDirection . Ascending :
54+ this . _myRemoteOrDbDataSourceAsyncProxy . SortDescriptionList . Add (
55+ new SortDescription ( sortMemberPath , ListSortDirection . Ascending ) ) ;
56+ break ;
57+ case ListSortDirection . Descending :
58+ this . _myRemoteOrDbDataSourceAsyncProxy . SortDescriptionList . Add (
59+ new SortDescription ( sortMemberPath , ListSortDirection . Descending ) ) ;
60+ break ;
3861 }
62+
63+ this . _myRemoteOrDbDataSourceAsyncProxy . FilterDescriptionList . OnCollectionReset ( ) ;
64+ this . _myDataVirtualizedAsyncFilterSortObservableCollection . Clear ( ) ;
65+ }
66+
67+ private async Task Filter ( MemberPathFilterText memberPathFilterText )
68+ {
69+ if ( string . IsNullOrWhiteSpace ( memberPathFilterText . FilterText ) )
70+ this . _myRemoteOrDbDataSourceAsyncProxy . FilterDescriptionList . Remove ( memberPathFilterText
71+ . MemberPath ) ;
3972 else
40- {
41- this . _myRemoteOrDbDataSourceAsyncProxy . FilterDescriptionList . Add ( new FilterDescription ( memberPathFilterText . ColumnSortMemberPath , memberPathFilterText . FilterText ) ) ;
42- }
73+ this . _myRemoteOrDbDataSourceAsyncProxy . FilterDescriptionList . Add (
74+ new FilterDescription ( memberPathFilterText . MemberPath , memberPathFilterText . FilterText ) ) ;
4375 Interlocked . Increment ( ref this . _filterWaitingCount ) ;
4476 await Task . Delay ( 500 ) ;
4577 if ( Interlocked . Decrement ( ref this . _filterWaitingCount ) != 0 ) return ;
4678 this . _myRemoteOrDbDataSourceAsyncProxy . FilterDescriptionList . OnCollectionReset ( ) ;
47- this . myDataVirtualizedAsyncFilterSortObservableCollection . Clear ( ) ;
79+ this . _myDataVirtualizedAsyncFilterSortObservableCollection . Clear ( ) ;
4880 }
49-
50- public ICollectionView MyDataVirtualizedAsyncFilterSortObservableCollectionCollectionView { get ; }
51-
52- public RelayCommand < object > FilterCommand { get ; }
5381 }
5482}
0 commit comments