Skip to content
This repository was archived by the owner on Oct 13, 2025. It is now read-only.

Commit feb3c8e

Browse files
committed
Enhance filtering system by introducing customizable text filtering options and improving filter control creation
1 parent 0fed5d0 commit feb3c8e

File tree

9 files changed

+264
-195
lines changed

9 files changed

+264
-195
lines changed

samples/TreeDataGridDemo/ViewModels/CountriesPageViewModel.cs

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,54 +25,49 @@ public CountriesPageViewModel()
2525
{
2626
// Text column with advanced filter
2727
new TextColumn<Country, string?>(
28-
"Country",
29-
x => x.Name,
30-
(r, v) => r.Name = v,
31-
new GridLength(6, GridUnitType.Star),
28+
"Country",
29+
x => x.Name,
30+
(r, v) => r.Name = v,
31+
new GridLength(6, GridUnitType.Star),
3232
new TextColumnOptions<Country>
3333
{
3434
IsTextSearchEnabled = true,
3535
IsFilterEnabled = true // This will create a TextFilter automatically
3636
}),
37-
37+
3838
// Use a template column with filtering for region
3939
new TemplateColumn<Country>(
40-
"Region",
40+
"Region",
4141
"RegionCell",
4242
"RegionEditCell",
4343
new GridLength(3, GridUnitType.Star),
4444
new TemplateColumnOptions<Country>
4545
{
46-
IsFilterEnabled = true,
47-
FilterValueSelector = x => x.Region // Value selector for filtering
46+
// Value selector for filtering
47+
FilterValueSelector = x => x.Region,
48+
Filter = new TextValueFilter(),
49+
FilterControlFactory = (col) => new TextFilterControl(col, "custom filter..."),
4850
}),
49-
51+
5052
// Population column with numeric filtering
5153
new TextColumn<Country, int>(
52-
"Population",
53-
x => x.Population,
54-
new GridLength(3, GridUnitType.Star),
55-
new TextColumnOptions<Country>
56-
{
57-
IsFilterEnabled = true
58-
}),
59-
54+
"Population",
55+
x => x.Population,
56+
new GridLength(3, GridUnitType.Star),
57+
new TextColumnOptions<Country> { IsFilterEnabled = true }),
58+
6059
// Area column with numeric filtering
6160
new TextColumn<Country, int>(
62-
"Area",
63-
x => x.Area,
64-
new GridLength(3, GridUnitType.Star),
65-
new TextColumnOptions<Country>
66-
{
67-
IsFilterEnabled = true,
68-
StringFormat = "{0:N0}"
69-
}),
70-
61+
"Area",
62+
x => x.Area,
63+
new GridLength(3, GridUnitType.Star),
64+
new TextColumnOptions<Country> { IsFilterEnabled = true, StringFormat = "{0:N0}" }),
65+
7166
// GDP column
7267
new TextColumn<Country, int>(
73-
"GDP",
74-
x => x.GDP,
75-
new GridLength(3, GridUnitType.Star),
68+
"GDP",
69+
x => x.GDP,
70+
new GridLength(3, GridUnitType.Star),
7671
new TextColumnOptions<Country>
7772
{
7873
TextAlignment = TextAlignment.Right,

src/Avalonia.Controls.TreeDataGrid/Models/TreeDataGrid/CheckBoxColumnOptions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22

33
namespace Avalonia.Controls.Models.TreeDataGrid
44
{
@@ -30,11 +30,11 @@ public class CheckBoxColumnOptions<TModel> : ColumnOptions<TModel>, IFilterContr
3030
/// <param name="column">The column for which to create a filter control.</param>
3131
/// <param name="initialValue">The initial filter value.</param>
3232
/// <returns>A filter control that can be used to filter the column.</returns>
33-
public IFilterControl? CreateFilterControl(IColumn column, object? initialValue)
33+
public IFilterControl? CreateFilterControl(IColumn column)
3434
{
35-
if (column is CheckBoxColumn<TModel> && IsFilterEnabled)
35+
if (column is CheckBoxColumn<TModel> col && IsFilterEnabled)
3636
{
37-
return new CheckBoxFilterControl(IsThreeStateFilter, initialValue);
37+
return new CheckBoxFilterControl(col, IsThreeStateFilter, null);
3838
}
3939

4040
return null;

src/Avalonia.Controls.TreeDataGrid/Models/TreeDataGrid/Filters.cs

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,60 @@
44

55
namespace Avalonia.Controls.Models.TreeDataGrid;
66

7+
/// <summary>
8+
/// Defines the mode of text filtering.
9+
/// </summary>
10+
public enum TextFilterMode
11+
{
12+
/// <summary>
13+
/// Check if the value contains the filter text.
14+
/// </summary>
15+
Contains,
16+
17+
/// <summary>
18+
/// Check if the value starts with the filter text.
19+
/// </summary>
20+
StartsWith,
21+
22+
/// <summary>
23+
/// Check if the value ends with the filter text.
24+
/// </summary>
25+
EndsWith
26+
}
27+
728
/// <summary>
829
/// A filter that checks text values.
930
/// </summary>
1031
public class TextValueFilter : IValueFilter<string>
1132
{
33+
/// <summary>
34+
/// Gets or sets the filter mode to use.
35+
/// </summary>
36+
public TextFilterMode FilterMode { get; set; } = TextFilterMode.Contains;
37+
38+
/// <summary>
39+
/// Gets or sets whether filtering is case sensitive.
40+
/// </summary>
41+
public bool CaseSensitive { get; set; } = false;
42+
43+
/// <summary>
44+
/// Initializes a new instance of <see cref="TextValueFilter"/> with default settings.
45+
/// </summary>
46+
public TextValueFilter()
47+
{
48+
}
49+
50+
/// <summary>
51+
/// Initializes a new instance of <see cref="TextValueFilter"/> with specified filter mode.
52+
/// </summary>
53+
/// <param name="mode">The filter mode to use.</param>
54+
/// <param name="caseSensitive">Whether filtering is case sensitive.</param>
55+
public TextValueFilter(TextFilterMode mode, bool caseSensitive = false)
56+
{
57+
FilterMode = mode;
58+
CaseSensitive = caseSensitive;
59+
}
60+
1261
/// <summary>
1362
/// Determines if the value passes the filter.
1463
/// </summary>
@@ -19,8 +68,23 @@ public bool Passes(object? condition, string? value)
1968
{
2069
if (condition is not string filterText || string.IsNullOrWhiteSpace(filterText))
2170
return true;
22-
23-
return value != null && value.Contains(filterText, StringComparison.OrdinalIgnoreCase);
71+
72+
if (value == null)
73+
return false;
74+
75+
// Determine string comparison
76+
StringComparison comparison = CaseSensitive ?
77+
StringComparison.Ordinal :
78+
StringComparison.OrdinalIgnoreCase;
79+
80+
// Apply filter based on mode
81+
return FilterMode switch
82+
{
83+
TextFilterMode.Contains => value.Contains(filterText, comparison),
84+
TextFilterMode.StartsWith => value.StartsWith(filterText, comparison),
85+
TextFilterMode.EndsWith => value.EndsWith(filterText, comparison),
86+
_ => value.Contains(filterText, comparison) // Default to Contains
87+
};
2488
}
2589
}
2690

0 commit comments

Comments
 (0)