Skip to content

Commit 210fdba

Browse files
committed
Add Filtering.
1 parent 91b317d commit 210fdba

15 files changed

Lines changed: 500 additions & 410 deletions

DataGridAsyncDemoMVVM/App.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88

99
<local:MainViewModel x:Key="MainViewModel" />
1010
</Application.Resources>
11-
</Application>
11+
</Application>

DataGridAsyncDemoMVVM/App.xaml.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Configuration;
4-
using System.Data;
5-
using System.Linq;
6-
using System.Threading.Tasks;
7-
using System.Windows;
1+
using System.Windows;
82

93
namespace DataGridAsyncDemoMVVM
104
{
115
/// <summary>
12-
/// Interaction logic for App.xaml
6+
/// Interaction logic for App.xaml
137
/// </summary>
148
public partial class App : Application
159
{
1610
}
17-
}
11+
}

DataGridAsyncDemoMVVM/DataGridAsyncDemoMVVM.csproj

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,26 @@
3434
<WarningLevel>4</WarningLevel>
3535
</PropertyGroup>
3636
<ItemGroup>
37+
<Reference Include="GalaSoft.MvvmLight, Version=5.3.0.19026, Culture=neutral, PublicKeyToken=e7570ab207bcb616, processorArchitecture=MSIL">
38+
<HintPath>..\packages\MvvmLightLibs.5.3.0.0\lib\net45\GalaSoft.MvvmLight.dll</HintPath>
39+
</Reference>
40+
<Reference Include="GalaSoft.MvvmLight.Extras, Version=5.3.0.19032, Culture=neutral, PublicKeyToken=669f0b5e8f868abf, processorArchitecture=MSIL">
41+
<HintPath>..\packages\MvvmLightLibs.5.3.0.0\lib\net45\GalaSoft.MvvmLight.Extras.dll</HintPath>
42+
</Reference>
43+
<Reference Include="GalaSoft.MvvmLight.Platform, Version=5.3.0.19032, Culture=neutral, PublicKeyToken=5f873c45e98af8a1, processorArchitecture=MSIL">
44+
<HintPath>..\packages\MvvmLightLibs.5.3.0.0\lib\net45\GalaSoft.MvvmLight.Platform.dll</HintPath>
45+
</Reference>
46+
<Reference Include="Microsoft.Practices.ServiceLocation, Version=1.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
47+
<HintPath>..\packages\CommonServiceLocator.1.3\lib\portable-net4+sl5+netcore45+wpa81+wp8\Microsoft.Practices.ServiceLocation.dll</HintPath>
48+
</Reference>
3749
<Reference Include="System" />
3850
<Reference Include="System.Data" />
3951
<Reference Include="System.Linq.Dynamic">
4052
<HintPath>..\packages\System.Linq.Dynamic.1.0.7\lib\net40\System.Linq.Dynamic.dll</HintPath>
4153
</Reference>
54+
<Reference Include="System.Windows.Interactivity, Version=4.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
55+
<HintPath>..\packages\MvvmLightLibs.5.3.0.0\lib\net45\System.Windows.Interactivity.dll</HintPath>
56+
</Reference>
4257
<Reference Include="System.Xml" />
4358
<Reference Include="Microsoft.CSharp" />
4459
<Reference Include="System.Core" />
@@ -57,6 +72,7 @@
5772
<Generator>MSBuild:Compile</Generator>
5873
<SubType>Designer</SubType>
5974
</ApplicationDefinition>
75+
<Compile Include="filtersort\MemberPathFilterText.cs" />
6076
<Compile Include="filtersort\DescriptionList.cs" />
6177
<Compile Include="filtersort\FilterDescription.cs" />
6278
<Compile Include="filtersort\FilterDescriptionList.cs" />
@@ -69,6 +85,7 @@
6985
<Compile Include="RemoteOrDbDataItem.cs" />
7086
<Compile Include="RemoteOrDbDataSourceAsyncProxy.cs" />
7187
<Compile Include="RemoteOrDbDataSourceEmulation.cs" />
88+
<Compile Include="converters\TextChangedEventArgsToDatagridHeaderAndText.cs" />
7289
<Page Include="MainWindow.xaml">
7390
<Generator>MSBuild:Compile</Generator>
7491
<SubType>Designer</SubType>
Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,54 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
2+
using System.ComponentModel;
3+
using System.Diagnostics;
4+
using System.Threading;
55
using System.Threading.Tasks;
6+
using System.Windows.Data;
67
using AlphaChiTech.Virtualization;
8+
using DataGridAsyncDemoMVVM.filtersort;
9+
using GalaSoft.MvvmLight.Command;
710

811
namespace DataGridAsyncDemoMVVM
912
{
10-
class MainViewModel
13+
internal class MainViewModel
1114
{
12-
private VirtualizingObservableCollection<RemoteOrDbDataItem> _myDataVirtualizedAsyncFilterSortObservableCollection = null;
13-
private RemoteOrDbDataSourceAsyncProxy _myRemoteOrDbDataSourceAsyncProxy = null;
15+
private readonly RemoteOrDbDataSourceAsyncProxy _myRemoteOrDbDataSourceAsyncProxy;
16+
private VirtualizingObservableCollection<RemoteOrDbDataItem> myDataVirtualizedAsyncFilterSortObservableCollection;
1417

15-
public VirtualizingObservableCollection<RemoteOrDbDataItem> MyDataVirtualizedAsyncFilterSortObservableCollection
18+
public MainViewModel()
1619
{
17-
get
20+
this._myRemoteOrDbDataSourceAsyncProxy = new RemoteOrDbDataSourceAsyncProxy(new RemoteOrDbDataSourceEmulation(100));
21+
this.myDataVirtualizedAsyncFilterSortObservableCollection =
22+
new VirtualizingObservableCollection<RemoteOrDbDataItem>(
23+
new PaginationManager<RemoteOrDbDataItem>(this._myRemoteOrDbDataSourceAsyncProxy,
24+
pageSize: 10, maxPages: 2));
25+
this.MyDataVirtualizedAsyncFilterSortObservableCollectionCollectionView =
26+
CollectionViewSource.GetDefaultView(myDataVirtualizedAsyncFilterSortObservableCollection);
27+
28+
this.FilterCommand = new RelayCommand<object>(async o => await this.Filter(o as MemberPathFilterText));
29+
}
30+
31+
private int _filterWaitingCount = 0;
32+
private async Task Filter(MemberPathFilterText memberPathFilterText)
33+
{
34+
if (String.IsNullOrWhiteSpace(memberPathFilterText.FilterText))
1835
{
19-
if (this._myDataVirtualizedAsyncFilterSortObservableCollection == null)
20-
{
21-
this._myRemoteOrDbDataSourceAsyncProxy = new RemoteOrDbDataSourceAsyncProxy(new RemoteOrDbDataSourceEmulation());
22-
this._myDataVirtualizedAsyncFilterSortObservableCollection =
23-
new VirtualizingObservableCollection<RemoteOrDbDataItem>(
24-
new PaginationManager<RemoteOrDbDataItem>(this._myRemoteOrDbDataSourceAsyncProxy, pageSize: 10, maxPages: 2));
25-
}
26-
return this._myDataVirtualizedAsyncFilterSortObservableCollection;
36+
this._myRemoteOrDbDataSourceAsyncProxy.FilterDescriptionList.Remove(memberPathFilterText
37+
.ColumnSortMemberPath);
2738
}
39+
else
40+
{
41+
this._myRemoteOrDbDataSourceAsyncProxy.FilterDescriptionList.Add(new FilterDescription(memberPathFilterText.ColumnSortMemberPath, memberPathFilterText.FilterText));
42+
}
43+
Interlocked.Increment(ref this._filterWaitingCount);
44+
await Task.Delay(500);
45+
if (Interlocked.Decrement(ref this._filterWaitingCount) != 0) return;
46+
this._myRemoteOrDbDataSourceAsyncProxy.FilterDescriptionList.OnCollectionReset();
47+
this.myDataVirtualizedAsyncFilterSortObservableCollection.Clear();
2848
}
49+
50+
public ICollectionView MyDataVirtualizedAsyncFilterSortObservableCollectionCollectionView { get; }
51+
52+
public RelayCommand<object> FilterCommand { get; }
2953
}
30-
}
54+
}

DataGridAsyncDemoMVVM/MainWindow.xaml

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
<Window x:Class="DataGridAsyncDemoMVVM.MainWindow"
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Platform"
5+
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
6+
xmlns:dataGridAsyncDemoMvvm="clr-namespace:DataGridAsyncDemoMVVM"
7+
xmlns:converters="clr-namespace:DataGridAsyncDemoMVVM.converters"
48
Title="TheMainWindow"
59
Width="525"
610
Height="350"
7-
DataContext="{StaticResource MainViewModel}"
8-
>
11+
DataContext="{StaticResource MainViewModel}">
12+
<Window.Resources>
13+
<converters:TextChangedEventArgsToDatagridHeaderAndText x:Key="TextChangedEventArgsToDatagridHeaderAndText" />
14+
</Window.Resources>
915
<Grid>
1016
<Grid.RowDefinitions>
1117
<RowDefinition Height="Auto" />
@@ -15,7 +21,7 @@
1521
<DataGrid x:Name="TstDataGridAsyncFilterSort"
1622
Grid.Row="1"
1723
CanUserSortColumns="True"
18-
ItemsSource="{Binding MyDataVirtualizedAsyncFilterSortObservableCollection}">
24+
ItemsSource="{Binding MyDataVirtualizedAsyncFilterSortObservableCollectionCollectionView}">
1925

2026
<DataGrid.ColumnHeaderStyle>
2127
<Style TargetType="DataGridColumnHeader">
@@ -38,7 +44,18 @@
3844
VerticalContentAlignment="Stretch"
3945
Background="GhostWhite"
4046
BorderThickness="0"
41-
Padding="0" />
47+
Padding="0">
48+
<i:Interaction.Triggers>
49+
<i:EventTrigger EventName="TextChanged">
50+
<cmd:EventToCommand
51+
Command="{Binding Mode=OneWay,
52+
Path=DataContext.FilterCommand,
53+
RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
54+
EventArgsConverter="{StaticResource TextChangedEventArgsToDatagridHeaderAndText}"
55+
PassEventArgsToCommand="True" />
56+
</i:EventTrigger>
57+
</i:Interaction.Triggers>
58+
</TextBox>
4259
</Grid>
4360
</DataTemplate>
4461
</Setter.Value>
@@ -47,4 +64,4 @@
4764
</DataGrid.ColumnHeaderStyle>
4865
</DataGrid>
4966
</Grid>
50-
</Window>
67+
</Window>

DataGridAsyncDemoMVVM/MainWindow.xaml.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System;
44
using System.Diagnostics;
55
using System.Windows;
6+
using System.Windows.Controls;
67
using System.Windows.Threading;
78
using AlphaChiTech.Virtualization;
89

DataGridAsyncDemoMVVM/Properties/Resources.Designer.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11
namespace DataGridAsyncDemoMVVM
22
{
3-
public class RemoteOrDbDataItem
4-
{
5-
public RemoteOrDbDataItem()
6-
{}
7-
8-
public RemoteOrDbDataItem( string name, string str1, string str2, int int1, double double1 )
3+
public class RemoteOrDbDataItem
94
{
10-
this.Name = name;
11-
this.Str1 = str1;
12-
this.Str2 = str2;
13-
this.Int1 = int1;
14-
this.Double1 = double1;
15-
}
5+
public RemoteOrDbDataItem()
6+
{
7+
}
168

17-
#region properties
9+
public RemoteOrDbDataItem(string name, string str1, string str2, int int1, double double1)
10+
{
11+
this.Name = name;
12+
this.Str1 = str1;
13+
this.Str2 = str2;
14+
this.Int1 = int1;
15+
this.Double1 = double1;
16+
}
1817

19-
public double Double1 { get; set; }
20-
public int Int1 { get; set; }
21-
public string Name { get; set; }
22-
public string Str1 { get; set; }
23-
public string Str2 { get; set; }
18+
#region properties
2419

25-
#endregion
26-
}
27-
}
20+
public double Double1 { get; set; }
21+
public int Int1 { get; set; }
22+
public string Name { get; set; }
23+
public string Str1 { get; set; }
24+
public string Str2 { get; set; }
25+
26+
#endregion
27+
}
28+
}

0 commit comments

Comments
 (0)