-
-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathTableViewFilterItemsControl.xaml.cs
More file actions
194 lines (168 loc) · 5.34 KB
/
Copy pathTableViewFilterItemsControl.xaml.cs
File metadata and controls
194 lines (168 loc) · 5.34 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Input;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using Windows.System;
namespace WinUI.TableView.Controls;
/// <summary>
/// Represents the control that displays filter items in the filter flyout of a TableViewColumnHeader.
/// </summary>
public partial class TableViewFilterItemsControl : UserControl
{
private bool _canSetState = true;
/// <summary>
/// Initializes a new instance of the <see cref="TableViewFilterItemsControl"/> class.
/// </summary>
public TableViewFilterItemsControl()
{
InitializeComponent();
}
/// <summary>
/// Initializes the state of the <see cref="TableViewFilterItemsControl"/>.
/// </summary>
internal async void Initialize()
{
FilterItems = TableView?.FilterHandler?.GetFilterItems(ColumnHeader?.Column!, null).ToList();
if (searchBox is not null)
{
await Task.Delay(100);
await FocusManager.TryFocusAsync(searchBox, FocusState.Programmatic);
}
if (filterItemsList is not null && filterItemsList.Items.Count > 0)
{
filterItemsList.ScrollIntoView(filterItemsList.Items[0]);
}
}
/// <summary>
/// Clears the search box text.
/// </summary>
internal void ClearSearchBox()
{
searchBox?.Text = string.Empty;
}
private void OnSearchBoxTextChanged(object sender, TextChangedEventArgs e)
{
FilterItems = TableView?.FilterHandler?.GetFilterItems(ColumnHeader?.Column!, searchBox!.Text);
}
/// <summary>
/// Handles the KeyDown or PreviewKeyDown event for the searchBox.
/// </summary>
private void OnSearchBoxKeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == VirtualKey.Enter && searchBox?.Text.Length > 0)
{
ColumnHeader?.HideFlyout();
ColumnHeader?.ApplyFilter();
e.Handled = true;
}
}
/// <summary>
/// Handles the Checked and Unchecked event for the selectAllCheckBox.
/// </summary>
private void OnSelectAllCheckBoxCheckChanged(object sender, RoutedEventArgs e)
{
SetFilterItemsState(selectAllCheckBox.IsChecked is true);
}
/// <summary>
/// Sets the state of the select all checkbox.
/// </summary>
internal void SetSelectAllCheckBoxState()
{
if (selectAllCheckBox is null || !_canSetState)
{
return;
}
selectAllCheckBox.IsChecked = FilterItems?.All(x => x.IsSelected) ?? false ? true
: FilterItems?.All(x => !x.IsSelected) ?? false ? false
: null;
}
/// <summary>
/// Sets the state of the filter items.
/// </summary>
/// <param name="isSelected">The state to set.</param>
internal void SetFilterItemsState(bool isSelected)
{
_canSetState = false;
foreach (var item in filterItemsList.Items.OfType<TableViewFilterItem>())
{
item.IsSelected = isSelected;
}
_canSetState = true;
}
/// <summary>
/// Attaches property changed handlers to the filter items.
/// </summary>
private void AttachPropertyChangedHandlers()
{
if (FilterItems?.Count > 0)
{
foreach (var item in FilterItems)
{
item.PropertyChanged += OnFilterItemPropertyChanged;
}
}
}
/// <summary>
/// Detaches property changed handlers from the filter items.
/// </summary>
private void DetachPropertyChangedHandlers()
{
if (FilterItems?.Count > 0)
{
foreach (var item in FilterItems)
{
item.PropertyChanged -= OnFilterItemPropertyChanged;
}
}
}
/// <summary>
/// Handles the PropertyChanged event for filter items.
/// </summary>
private void OnFilterItemPropertyChanged(object? sender, PropertyChangedEventArgs e)
{
SetSelectAllCheckBoxState();
}
/// <summary>
/// Gets a value indicating whether to apply the filter based on the control state.
/// </summary>
internal bool ShouldApplyFilter => selectAllCheckBox.IsChecked is not true || !string.IsNullOrEmpty(searchBox.Text);
/// <summary>
/// Gets or sets the filter items for the control.
/// </summary>
internal ICollection<TableViewFilterItem>? FilterItems
{
get;
set
{
if (field == value) return;
DetachPropertyChangedHandlers();
field = value;
filterItemsList.ItemsSource = field;
AttachPropertyChangedHandlers();
SetSelectAllCheckBoxState();
}
}
/// <summary>
/// Gets or sets the column header associated with the filter items control.
/// </summary>
public TableViewColumnHeader? ColumnHeader
{
get;
set
{
if (value is { FilterItemsControl: null })
{
value.FilterItemsControl = this;
}
field = value;
}
}
/// <summary>
/// Gets or sets the TableView associated with the filter items control.
/// </summary>
public TableView? TableView { get; internal set; }
}