Skip to content

Commit 7badd98

Browse files
committed
Add item count display to TableView filter flyout
1 parent a0ee89f commit 7badd98

5 files changed

Lines changed: 70 additions & 28 deletions

src/ColumnFilterHandler.cs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,25 @@ public virtual IList<TableViewFilterItem> GetFilterItems(TableViewColumn column,
3030
column.TableView.FilterDescriptions.Where(
3131
x => x is not ColumnFilterDescription columnFilter || columnFilter.Column != column));
3232

33-
var filterValues = new SortedSet<object?>();
34-
35-
foreach (var item in collectionView)
36-
{
37-
var value = column.GetCellContent(item);
38-
filterValues.Add(IsBlank(value) ? null : value);
39-
}
40-
41-
return [.. filterValues.Select(value =>
42-
{
43-
value ??= TableViewLocalizedStrings.BlankFilterValue;
44-
var isSelected = !column.IsFiltered || !string.IsNullOrEmpty(searchText) ||
45-
(column.IsFiltered && SelectedValues[column].Contains(value));
46-
47-
return string.IsNullOrEmpty(searchText)
48-
|| value?.ToString()?.Contains(searchText, StringComparison.OrdinalIgnoreCase) == true
49-
? new TableViewFilterItem(isSelected, value)
50-
: null;
51-
52-
}).OfType<TableViewFilterItem>()];
33+
return [.. collectionView.Select(column.GetCellContent)
34+
.Select(x => IsBlank(x) ? null : x)
35+
.GroupBy(x => x)
36+
.OrderBy(x => x.Key)
37+
.Select(x =>
38+
{
39+
var value = x.Key;
40+
value ??= TableViewLocalizedStrings.BlankFilterValue;
41+
var isSelected = !column.IsFiltered || !string.IsNullOrEmpty(searchText) ||
42+
(column.IsFiltered && SelectedValues[column].Contains(value));
43+
44+
return string.IsNullOrEmpty(searchText)
45+
|| value?.ToString()?.Contains(searchText, StringComparison.OrdinalIgnoreCase) == true
46+
? new TableViewFilterItem(isSelected, value, x.Count())
47+
: null;
48+
49+
})
50+
.OfType<TableViewFilterItem>()
51+
.OrderByDescending(x => _tableView.ShowFilterItemCounts ? x.Count : 0)];
5352
}
5453

5554
return [];

src/TableView.Properties.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,11 @@ public partial class TableView
260260
/// </summary>
261261
public static readonly DependencyProperty ConditionalCellStylesProperty = DependencyProperty.Register(nameof(ConditionalCellStyles), typeof(IList<TableViewConditionalCellStyle>), typeof(TableView), new PropertyMetadata(default));
262262

263+
/// <summary>
264+
/// Identifies the <see cref="ShowFilterItemCounts"/> dependency property.
265+
/// </summary>
266+
public static readonly DependencyProperty ShowFilterItemCountsProperty = DependencyProperty.Register(nameof(ShowFilterItemCounts), typeof(bool), typeof(TableView), new PropertyMetadata(false));
267+
263268
/// <summary>
264269
/// Gets or sets a value indicating whether opening the column filter over header right-click is enabled.
265270
/// </summary>
@@ -316,6 +321,15 @@ public IList<TableViewConditionalCellStyle> ConditionalCellStyles
316321
set => SetValue(ConditionalCellStylesProperty, value);
317322
}
318323

324+
/// <summary>
325+
/// Gets or sets a value that indicates whether the TableView displays item counts next to each filter item in filter flyout.
326+
/// </summary>
327+
public bool ShowFilterItemCounts
328+
{
329+
get => (bool)GetValue(ShowFilterItemCountsProperty);
330+
set => SetValue(ShowFilterItemCountsProperty, value);
331+
}
332+
319333
/// <summary>
320334
/// Gets or sets the selection start cell slot.
321335
/// </summary>

src/TableViewColumnHeader.FilterItem.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace WinUI.TableView;
55
/// <summary>
66
/// Represents a filter item used in the options flyout of a TableViewColumnHeader.
77
/// </summary>
8-
public partial class TableViewFilterItem : INotifyPropertyChanged
8+
public partial class TableViewFilterItem : INotifyPropertyChanged
99
{
1010
/// <inheritdoc/>
1111
public event PropertyChangedEventHandler? PropertyChanged;
@@ -17,10 +17,12 @@ public partial class TableViewFilterItem : INotifyPropertyChanged
1717
/// </summary>
1818
/// <param name="isSelected">Indicates whether the filter item is selected.</param>
1919
/// <param name="value">The value of the filter item.</param>
20-
public TableViewFilterItem(bool isSelected, object value)
20+
/// <param name="count">The count of occurrences for the filter item.</param>
21+
public TableViewFilterItem(bool isSelected, object value, int count = 1)
2122
{
2223
IsSelected = isSelected;
2324
Value = value;
25+
Count = count;
2426
}
2527

2628
/// <summary>
@@ -40,4 +42,9 @@ public bool IsSelected
4042
/// Gets the value of the filter item.
4143
/// </summary>
4244
public object Value { get; }
45+
46+
/// <summary>
47+
/// Gets or sets the count of occurrences for the filter item.
48+
/// </summary>
49+
public int Count { get; set; }
4350
}

src/TableViewColumnHeader.OptionsFlyoutViewModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public partial class TableViewColumnHeader
1212
/// <summary>
1313
/// ViewModel for the options flyout in the TableViewColumnHeader.
1414
/// </summary>
15-
private partial class OptionsFlyoutViewModel : INotifyPropertyChanged
15+
internal partial class OptionsFlyoutViewModel : INotifyPropertyChanged
1616
{
1717
public event PropertyChangedEventHandler? PropertyChanged;
1818
private IList<TableViewFilterItem> _filterItems = [];
@@ -114,7 +114,7 @@ public IList<TableViewFilterItem> FilterItems
114114

115115
DetachPropertyChangedHandlers();
116116
_filterItems = value;
117-
AttachPropertyChangedHandlers();
117+
AttachPropertyChangedHandlers();
118118
SetSelectAllCheckBoxState();
119119
OnPropertyChanged();
120120
}

src/Themes/TableViewColumnHeader.xaml

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44
xmlns:local="using:WinUI.TableView"
55
xmlns:win="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
66
xmlns:not_win="http://uno.ui/not_win"
7+
xmlns:converters="using:WinUI.TableView.Converters"
78
mc:Ignorable="not_win">
89

910
<ResourceDictionary.MergedDictionaries>
1011
<ResourceDictionary Source="ms-appx:///WinUI.TableView/Themes/Resources.xaml" />
1112
</ResourceDictionary.MergedDictionaries>
1213

14+
<converters:BoolToVisibilityConverter x:Key="BoolToVisibility" />
15+
1316
<Style x:Key="DefaultTableViewColumnHeaderStyle"
1417
TargetType="local:TableViewColumnHeader">
1518
<Setter Property="FontWeight"
@@ -152,7 +155,7 @@
152155
<MenuFlyoutItem.Template>
153156
<ControlTemplate TargetType="MenuFlyoutItem">
154157
<Grid Margin="8,4"
155-
Width="220"
158+
Width="250"
156159
Height="300">
157160
<Grid.RowDefinitions>
158161
<RowDefinition Height="Auto" />
@@ -182,13 +185,32 @@
182185
Value="36" />
183186
<Setter Property="Height"
184187
Value="36" />
188+
<Setter Property="HorizontalAlignment"
189+
Value="Stretch" />
190+
<Setter Property="HorizontalContentAlignment"
191+
Value="Stretch" />
185192
</Style>
186193
</ListView.ItemContainerStyle>
187194
<ListView.ItemTemplate>
188195
<DataTemplate>
189-
<CheckBox IsChecked="{Binding IsSelected, Mode=TwoWay}">
190-
<TextBlock Text="{Binding Value}"
191-
TextWrapping="NoWrap" />
196+
<CheckBox IsChecked="{Binding IsSelected, Mode=TwoWay}"
197+
HorizontalAlignment="Stretch"
198+
HorizontalContentAlignment="Stretch">
199+
<Grid>
200+
<Grid.ColumnDefinitions>
201+
<ColumnDefinition Width="*" />
202+
<ColumnDefinition Width="Auto" />
203+
</Grid.ColumnDefinitions>
204+
205+
<TextBlock Text="{Binding Value}"
206+
TextWrapping="NoWrap" />
207+
208+
<TextBlock Grid.Column="1"
209+
Visibility="{Binding DataContext.TableView.ShowFilterItemCounts, ElementName=FilterItemsList, Converter={StaticResource BoolToVisibility}}">
210+
<Run Text="Count:" />
211+
<Run Text="{Binding Count}" />
212+
</TextBlock>
213+
</Grid>
192214
</CheckBox>
193215
</DataTemplate>
194216
</ListView.ItemTemplate>

0 commit comments

Comments
 (0)