Skip to content

Commit adc8584

Browse files
committed
add wildcards
1 parent 14d4688 commit adc8584

File tree

6 files changed

+47
-43
lines changed

6 files changed

+47
-43
lines changed

SimpleDataGrid.Example/App.xaml.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
using System.Configuration;
2-
using System.Data;
3-
using System.Windows;
1+
using System.Windows;
42

53
namespace SimpleDataGrid.Example;
64

75
/// <summary>
86
/// Interaction logic for App.xaml
97
/// </summary>
10-
public partial class App : Application
11-
{
12-
}
8+
public partial class App : Application;
139

SimpleDataGrid.Example/MainViewModel.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
21
using SimpleDataGrid.Pagination;
3-
using System.Collections.Generic;
42

53
namespace SimpleDataGrid.Example;
64

@@ -14,10 +12,10 @@ public MainViewModel()
1412
People.SetSource(GetPeople());
1513
}
1614

17-
private List<Person> GetPeople()
15+
private static List<Person> GetPeople()
1816
{
1917
var people = new List<Person>();
20-
for (int i = 1; i <= 100; i++)
18+
for (var i = 1; i <= 100; i++)
2119
{
2220
people.Add(new Person { Id = i, Name = $"Person {i}", Age = 20 + (i % 50) });
2321
}

SimpleDataGrid.Example/MainWindow.xaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
<Window x:Class="SimpleDataGrid.Example.MainWindow"
23
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
34
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
@@ -19,6 +20,7 @@
1920
<StackPanel Grid.Row="0" Orientation="Horizontal" Margin="5">
2021
<TextBox x:Name="SearchTextBox" Width="200" Margin="5" />
2122
<Button Content="Search" Click="SearchButton_Click" Margin="5" />
23+
<CheckBox x:Name="WildcardCheckBox" Content="Use Wildcards" VerticalAlignment="Center" Margin="5" />
2224
</StackPanel>
2325

2426
<local:PersonPagedDataGrid x:Name="PagedDataGrid" Grid.Row="1" PagedSource="{Binding People}" AutoGenerateColumns="True" />
@@ -31,4 +33,4 @@
3133
<Button Content="Next" Click="NextButton_Click" Margin="5" />
3234
</StackPanel>
3335
</Grid>
34-
</Window>
36+
</Window>
Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,29 @@
1-
21
using SimpleDataGrid.Controls;
32
using System.Windows;
43

5-
namespace SimpleDataGrid.Example
6-
{
7-
public partial class MainWindow : Window
8-
{
9-
public MainWindow()
10-
{
11-
InitializeComponent();
12-
}
4+
namespace SimpleDataGrid.Example;
135

14-
private void PreviousButton_Click(object sender, RoutedEventArgs e)
15-
{
16-
var viewModel = (MainViewModel)DataContext;
17-
viewModel.People.PreviousPage();
18-
}
6+
public partial class MainWindow : Window
7+
{
8+
public MainWindow() => InitializeComponent();
199

20-
private void NextButton_Click(object sender, RoutedEventArgs e)
21-
{
22-
var viewModel = (MainViewModel)DataContext;
23-
viewModel.People.NextPage();
24-
}
10+
private void PreviousButton_Click(object sender, RoutedEventArgs e)
11+
{
12+
var viewModel = (MainViewModel)DataContext;
13+
viewModel.People.PreviousPage();
14+
}
2515

26-
private void SearchButton_Click(object sender, RoutedEventArgs e)
27-
{
28-
var viewModel = (MainViewModel)DataContext;
29-
viewModel.People.SetSearch(p => p.Name, SearchTextBox.Text);
30-
}
16+
private void NextButton_Click(object sender, RoutedEventArgs e)
17+
{
18+
var viewModel = (MainViewModel)DataContext;
19+
viewModel.People.NextPage();
3120
}
3221

33-
public class PersonPagedDataGrid : PagedDataGrid<Person> { }
22+
private void SearchButton_Click(object sender, RoutedEventArgs e)
23+
{
24+
var viewModel = (MainViewModel)DataContext;
25+
viewModel.People.SetSearch(p => p.Name, SearchTextBox.Text, WildcardCheckBox.IsChecked == true);
26+
}
3427
}
28+
29+
public class PersonPagedDataGrid : PagedDataGrid<Person>;

SimpleDataGrid/GlobalUsings.cs

Lines changed: 0 additions & 1 deletion
This file was deleted.

SimpleDataGrid/Pagination/PagedCollection.cs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
21
using System.ComponentModel;
2+
using System.Text.RegularExpressions;
33

44
namespace SimpleDataGrid.Pagination;
55

@@ -17,6 +17,7 @@ public class PagedCollection<T> : INotifyPropertyChanged
1717
private readonly List<Func<T, bool>> _filters = [];
1818
private Func<T, string>? _searchSelector;
1919
private string? _searchTerm;
20+
private bool _useWildcards;
2021

2122
/// <summary>
2223
/// Initializes a new instance of the <see cref="PagedCollection{T}"/> class.
@@ -62,10 +63,12 @@ public void ClearFilters()
6263
/// </summary>
6364
/// <param name="selector">A function that returns the string representation of the object to search.</param>
6465
/// <param name="term">The search term.</param>
65-
public void SetSearch(Func<T, string> selector, string? term)
66+
/// <param name="useWildcards">A value indicating whether to use wildcards in the search term.</param>
67+
public void SetSearch(Func<T, string> selector, string? term, bool useWildcards = false)
6668
{
6769
_searchSelector = selector;
6870
_searchTerm = term;
71+
_useWildcards = useWildcards;
6972
ApplyFiltering();
7073
}
7174

@@ -80,16 +83,27 @@ private void ApplyFiltering()
8083

8184
if (!string.IsNullOrWhiteSpace(_searchTerm) && _searchSelector != null)
8285
{
83-
query = query.Where(x =>
84-
_searchSelector(x)
85-
?.Contains(_searchTerm, StringComparison.OrdinalIgnoreCase) == true);
86+
if (_useWildcards)
87+
{
88+
var regex = new Regex(WildcardToRegex(_searchTerm), RegexOptions.IgnoreCase);
89+
query = query.Where(x => regex.IsMatch(_searchSelector(x)));
90+
}
91+
else
92+
{
93+
query = query.Where(x =>
94+
_searchSelector(x)
95+
?.Contains(_searchTerm, StringComparison.OrdinalIgnoreCase) == true);
96+
}
8697
}
8798

8899
_filtered = [.. query];
89100
_currentPage = 0;
90101
RaiseAllChanged();
91102
}
92103

104+
private static string WildcardToRegex(string pattern)
105+
=> "^" + Regex.Escape(pattern).Replace(@"\*", ".*").Replace(@"\?", ".") + "$";
106+
93107
/// <summary>
94108
/// Gets the items on the current page.
95109
/// </summary>
@@ -161,4 +175,4 @@ private void RaiseAllChanged()
161175
public event PropertyChangedEventHandler? PropertyChanged;
162176
private void OnPropertyChanged(string name) =>
163177
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
164-
}
178+
}

0 commit comments

Comments
 (0)