diff --git a/DataGridAsyncDemoMVVM/DataGridAsyncDemoMVVM.csproj b/DataGridAsyncDemoMVVM/DataGridAsyncDemoMVVM.csproj index 76088dd..0864128 100644 --- a/DataGridAsyncDemoMVVM/DataGridAsyncDemoMVVM.csproj +++ b/DataGridAsyncDemoMVVM/DataGridAsyncDemoMVVM.csproj @@ -72,6 +72,8 @@ MSBuild:Compile Designer + + @@ -85,7 +87,7 @@ - + MSBuild:Compile Designer diff --git a/DataGridAsyncDemoMVVM/MainViewModel.cs b/DataGridAsyncDemoMVVM/MainViewModel.cs index 7cfae0c..2dd288f 100644 --- a/DataGridAsyncDemoMVVM/MainViewModel.cs +++ b/DataGridAsyncDemoMVVM/MainViewModel.cs @@ -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 + _myDataVirtualizedAsyncFilterSortObservableCollection; + private readonly RemoteOrDbDataSourceAsyncProxy _myRemoteOrDbDataSourceAsyncProxy; - private VirtualizingObservableCollection 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( new PaginationManager(this._myRemoteOrDbDataSourceAsyncProxy, pageSize: 10, maxPages: 2)); this.MyDataVirtualizedAsyncFilterSortObservableCollectionCollectionView = - CollectionViewSource.GetDefaultView(myDataVirtualizedAsyncFilterSortObservableCollection); + CollectionViewSource.GetDefaultView(this._myDataVirtualizedAsyncFilterSortObservableCollection); - this.FilterCommand = new RelayCommand(async o => await this.Filter(o as MemberPathFilterText)); + this.FilterCommand = new RelayCommand(async o => await this.Filter(o)); + this.SortCommand = new RelayCommand(async o => await this.Sort(o)); } - private int _filterWaitingCount = 0; - private async Task Filter(MemberPathFilterText memberPathFilterText) + public ICollectionView MyDataVirtualizedAsyncFilterSortObservableCollectionCollectionView { get; } + + public RelayCommand FilterCommand { get; } + + public RelayCommand 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 FilterCommand { get; } } } \ No newline at end of file diff --git a/DataGridAsyncDemoMVVM/MainWindow.xaml b/DataGridAsyncDemoMVVM/MainWindow.xaml index 1508f5e..06a365d 100644 --- a/DataGridAsyncDemoMVVM/MainWindow.xaml +++ b/DataGridAsyncDemoMVVM/MainWindow.xaml @@ -10,7 +10,9 @@ Height="350" DataContext="{StaticResource MainViewModel}"> - + + @@ -22,7 +24,14 @@ Grid.Row="1" CanUserSortColumns="True" ItemsSource="{Binding MyDataVirtualizedAsyncFilterSortObservableCollectionCollectionView}"> - + + + + +