Skip to content

Commit 3eec55e

Browse files
committed
changed filter & filter items search logic
1 parent 4ea6ecc commit 3eec55e

File tree

3 files changed

+61
-64
lines changed

3 files changed

+61
-64
lines changed

src/TableViewColumnHeader.OptionsFlyoutViewModel.cs

Lines changed: 4 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,7 @@ private void InitializeCommands()
5252
OkCommand.ExecuteRequested += delegate
5353
{
5454
ColumnHeader.HideFlyout();
55-
56-
if (ColumnHeader!._selectAllCheckBox!.IsChecked is true && string.IsNullOrEmpty(FilterText))
57-
{
58-
ColumnHeader.ClearFilter();
59-
}
60-
else
61-
{
62-
SelectedValues = FilterItems.Where(x => x.IsSelected).Select(x => x.Value).ToList();
63-
ColumnHeader.ApplyFilter();
64-
}
55+
ColumnHeader.ApplyFilter();
6556
};
6657

6758
CancelCommand.ExecuteRequested += delegate { ColumnHeader.HideFlyout(); };
@@ -103,16 +94,6 @@ private void OnFilterItemPropertyChanged(object? sender, PropertyChangedEventArg
10394
SetSelectAllCheckBoxState();
10495
}
10596

106-
/// <summary>
107-
/// Clears the filter text.
108-
/// </summary>
109-
internal void ClearFilterText()
110-
{
111-
_canFilter = false;
112-
FilterText = default;
113-
_canFilter = true;
114-
}
115-
11697
/// <summary>
11798
/// Gets the TableView associated with the ViewModel.
11899
/// </summary>
@@ -123,30 +104,6 @@ internal void ClearFilterText()
123104
/// </summary>
124105
public TableViewColumnHeader ColumnHeader { get; }
125106

126-
/// <summary>
127-
/// Gets or sets the filter text.
128-
/// </summary>
129-
public string? FilterText
130-
{
131-
get => _filterText;
132-
set
133-
{
134-
_filterText = value;
135-
OnFilterItemsSearchTextChanged();
136-
OnPropertyChanged();
137-
}
138-
}
139-
140-
/// <summary>
141-
/// Handles the filter items search text changed event.
142-
/// </summary>
143-
private void OnFilterItemsSearchTextChanged()
144-
{
145-
if (!_canFilter) return;
146-
147-
FilterItems = TableView.FilterHandler.GetFilterItems(ColumnHeader.Column!, FilterText);
148-
}
149-
150107
/// <summary>
151108
/// Gets or sets the filter items.
152109
/// </summary>
@@ -168,7 +125,7 @@ public IList<TableViewFilterItem> FilterItems
168125
/// <summary>
169126
/// Gets the selected values for the filter.
170127
/// </summary>
171-
public List<object> SelectedValues { get; private set; } = [];
128+
public List<object> SelectedValues { get; set; } = [];
172129

173130
/// <summary>
174131
/// Sets the state of the select all checkbox.
@@ -220,7 +177,7 @@ private void OnPropertyChanged([CallerMemberName] string? propertyName = default
220177
/// <summary>
221178
/// Gets the command to sort in descending order.
222179
/// </summary>
223-
public StandardUICommand SortDescendingCommand { get; } = new() { Label = TableViewLocalizedStrings.SortDescending};
180+
public StandardUICommand SortDescendingCommand { get; } = new() { Label = TableViewLocalizedStrings.SortDescending };
224181

225182
/// <summary>
226183
/// Gets the command to clear sorting.
@@ -235,7 +192,7 @@ private void OnPropertyChanged([CallerMemberName] string? propertyName = default
235192
/// <summary>
236193
/// Gets the command to confirm the filter.
237194
/// </summary>
238-
public StandardUICommand OkCommand { get; } = new() { Label = TableViewLocalizedStrings.Ok};
195+
public StandardUICommand OkCommand { get; } = new() { Label = TableViewLocalizedStrings.Ok };
239196

240197
/// <summary>
241198
/// Gets the command to cancel the filter.

src/TableViewColumnHeader.cs

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public partial class TableViewColumnHeader : ContentControl
4242
private bool _resizeStarted;
4343
private double _resizeStartingWidth;
4444
private bool _resizePreviousStarted;
45+
private TextBox? _searchBox;
4546

4647
/// <summary>
4748
/// Initializes a new instance of the TableViewColumnHeader class.
@@ -125,10 +126,17 @@ private void ClearFilter()
125126
/// </summary>
126127
private void ApplyFilter()
127128
{
128-
if (_tableView is not null)
129+
if (_selectAllCheckBox?.IsChecked is true && string.IsNullOrEmpty(_searchBox?.Text))
129130
{
130-
_tableView.FilterHandler.SelectedValues[Column!] = _optionsFlyoutViewModel.SelectedValues;
131-
_tableView.FilterHandler?.ApplyFilter(Column!);
131+
ClearFilter();
132+
}
133+
else if (_tableView is not null)
134+
{
135+
_optionsFlyoutViewModel.SelectedValues = [.. _optionsFlyoutViewModel.FilterItems.Where(x => x.IsSelected).Select(x => x.Value)];
136+
{
137+
_tableView.FilterHandler.SelectedValues[Column!] = _optionsFlyoutViewModel.SelectedValues;
138+
_tableView.FilterHandler?.ApplyFilter(Column!);
139+
}
132140
}
133141
}
134142

@@ -186,7 +194,7 @@ protected override void OnApplyTemplate()
186194
}
187195

188196
_optionsFlyout.Opening += OnOptionsFlyoutOpening;
189-
_optionsFlyout.Closed += (s, e) => _optionsFlyoutViewModel.ClearFilterText();
197+
_optionsFlyout.Closed += OnOptionsFlyoutClosed;
190198
_optionsButton.Tapped += OnOptionsButtonTaped;
191199
_optionsButton.DataContext = _optionsFlyoutViewModel = new OptionsFlyoutViewModel(_tableView, this);
192200

@@ -208,26 +216,39 @@ protected override void OnApplyTemplate()
208216
}
209217
#endif
210218

211-
if (menuItem?.FindDescendant<AutoSuggestBox>(x => x.Name == "SearchBox") is { } searchBox)
219+
if (menuItem?.FindDescendant<TextBox>(x => x.Name == "SearchBox") is { } searchBox)
212220
{
213-
searchBox.PlaceholderText = TableViewLocalizedStrings.SearchBoxPlaceholder;
221+
_searchBox = searchBox;
222+
_searchBox.PlaceholderText = TableViewLocalizedStrings.SearchBoxPlaceholder;
223+
_searchBox.TextChanged += OnSearchBoxTextChanged;
214224
#if WINDOWS
215-
searchBox.PreviewKeyDown += OnSearchBoxKeyDown;
225+
_searchBox.PreviewKeyDown += OnSearchBoxKeyDown;
216226
#else
217-
searchBox.KeyDown += OnSearchBoxKeyDown;
227+
_searchBox.KeyDown += OnSearchBoxKeyDown;
218228
#endif
219229
}
220230

221231
SetFilterButtonVisibility();
222232
EnsureGridLines();
223233
}
224234

235+
/// <summary>
236+
/// Handles the TextChanged event for the search box.
237+
/// </summary>
238+
private void OnSearchBoxTextChanged(object sender, TextChangedEventArgs e)
239+
{
240+
if (_tableView?.FilterHandler is not null)
241+
{
242+
_optionsFlyoutViewModel.FilterItems = _tableView.FilterHandler.GetFilterItems(Column!, _searchBox!.Text);
243+
}
244+
}
245+
225246
/// <summary>
226247
/// Handles the KeyDown event for the search box.
227248
/// </summary>
228249
private void OnSearchBoxKeyDown(object sender, KeyRoutedEventArgs e)
229250
{
230-
if (e.Key == VirtualKey.Enter && _optionsFlyoutViewModel is { FilterText.Length: > 0 })
251+
if (e.Key == VirtualKey.Enter && _searchBox?.Text.Length > 0)
231252
{
232253
_optionsFlyoutViewModel.OkCommand.Execute(null);
233254

@@ -256,12 +277,29 @@ private void OnSelectAllCheckBoxUnchecked(object sender, RoutedEventArgs e)
256277
/// <summary>
257278
/// Handles the Opening event for the options flyout.
258279
/// </summary>
259-
private void OnOptionsFlyoutOpening(object? sender, object e)
280+
private async void OnOptionsFlyoutOpening(object? sender, object e)
260281
{
261282
if (_tableView?.FilterHandler is not null)
262283
{
263284
_optionsFlyoutViewModel.FilterItems = _tableView.FilterHandler.GetFilterItems(Column!, null);
264285
}
286+
287+
if (_searchBox is not null)
288+
{
289+
await Task.Delay(100);
290+
await FocusManager.TryFocusAsync(_searchBox, FocusState.Programmatic);
291+
}
292+
}
293+
294+
/// <summary>
295+
/// Handles the Closed event for the options flyout.
296+
/// </summary>
297+
private void OnOptionsFlyoutClosed(object? sender, object e)
298+
{
299+
if (_searchBox is not null)
300+
{
301+
_searchBox.Text = string.Empty;
302+
}
265303
}
266304

267305
/// <summary>

src/Themes/TableViewColumnHeader.xaml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
9999
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" />
100100

101-
<Grid HorizontalAlignment="Right">
101+
<Grid HorizontalAlignment="Right">
102102
<FontIcon x:Name="SortIcon"
103103
FontSize="10"
104104
Margin="0,2,0,0"
@@ -157,9 +157,7 @@
157157
<RowDefinition Height="Auto" />
158158
<RowDefinition Height="*" />
159159
</Grid.RowDefinitions>
160-
<AutoSuggestBox x:Name="SearchBox"
161-
QueryIcon="Find"
162-
Text="{Binding FilterText, Mode=TwoWay}" />
160+
<TextBox x:Name="SearchBox" />
163161
<Border Grid.Row="1"
164162
BorderThickness="0,0,0,1"
165163
BorderBrush="{ThemeResource MenuFlyoutSeparatorBackground}">
@@ -200,14 +198,18 @@
200198
<MenuFlyoutItem>
201199
<MenuFlyoutItem.Template>
202200
<ControlTemplate TargetType="MenuFlyoutItem">
203-
<Grid Margin="12,4"
201+
<Grid Margin="12,4"
204202
ColumnSpacing="16">
205203
<Grid.ColumnDefinitions>
206204
<ColumnDefinition Width="*" />
207205
<ColumnDefinition Width="*" />
208206
</Grid.ColumnDefinitions>
209-
<Button Command="{Binding OkCommand}" HorizontalAlignment="Stretch" Style="{StaticResource AccentButtonStyle}" />
210-
<Button Command="{Binding CancelCommand}" Grid.Column="1" HorizontalAlignment="Stretch" />
207+
<Button Command="{Binding OkCommand}"
208+
HorizontalAlignment="Stretch"
209+
Style="{StaticResource AccentButtonStyle}" />
210+
<Button Command="{Binding CancelCommand}"
211+
Grid.Column="1"
212+
HorizontalAlignment="Stretch" />
211213
</Grid>
212214
</ControlTemplate>
213215
</MenuFlyoutItem.Template>

0 commit comments

Comments
 (0)