-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathExternalFilteringPage.xaml.cs
More file actions
65 lines (53 loc) · 2 KB
/
Copy pathExternalFilteringPage.xaml.cs
File metadata and controls
65 lines (53 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using Microsoft.UI.Xaml.Controls;
namespace WinUI.TableView.SampleApp.Pages;
public sealed partial class ExternalFilteringPage : Page
{
private CancellationTokenSource? _token;
public ExternalFilteringPage()
{
InitializeComponent();
tableView.FilterDescriptions.Add(new FilterDescription(string.Empty, Filter));
Unloaded += OnPageUnloaded;
}
private bool Filter(object? item)
{
if (string.IsNullOrWhiteSpace(filterText.Text)) return true;
if (item is null) return false;
var model = (ExampleModel)item;
return model.FirstName?.Contains(filterText.Text, StringComparison.OrdinalIgnoreCase) is true ||
model.LastName?.Contains(filterText.Text, StringComparison.OrdinalIgnoreCase) is true ||
model.Email?.Contains(filterText.Text, StringComparison.OrdinalIgnoreCase) is true ||
model.Gender?.Contains(filterText.Text, StringComparison.OrdinalIgnoreCase) is true ||
model.Department?.Contains(filterText.Text, StringComparison.OrdinalIgnoreCase) is true ||
model.Designation?.Contains(filterText.Text, StringComparison.OrdinalIgnoreCase) is true ||
model.Address?.Contains(filterText.Text, StringComparison.OrdinalIgnoreCase) is true;
}
private async void OnSearchTextChanged(object sender, TextChangedEventArgs e)
{
if (_token is not null)
{
_token.Cancel();
}
_token = new CancellationTokenSource();
await RefreshFilter(_token.Token);
}
private async Task RefreshFilter(CancellationToken token)
{
try
{
await Task.Delay(200, token);
}
catch (OperationCanceledException)
{
return;
}
_token = null;
tableView.RefreshFilter();
}
private void OnPageUnloaded(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
{
_token?.Cancel();
_token?.Dispose();
_token = null;
}
}