Skip to content

Commit

Permalink
added new properties HeaderRowHeight, CanResizeColumns, CanSortColumn…
Browse files Browse the repository at this point in the history
…s, CanFilterColumns
  • Loading branch information
w-ahmad committed May 5, 2024
1 parent 8a4bab5 commit 8cd5c27
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 43 deletions.
71 changes: 71 additions & 0 deletions src/WinUI.TableView/TableView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,49 @@ private static void OnAutoGenerateColumnsChanged(DependencyObject d, DependencyP
}
}

private static void OnCanSortColumnsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is TableView tableView && e.NewValue is false)
{
tableView.ClearSorting();
}
}

private static void OnCanFilterColumnsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is TableView tableView && e.NewValue is false)
{
tableView.ClearFilters();
}
}

internal void ClearSorting()
{
CollectionView.SortDescriptions.Clear();

foreach (var header in Columns.Select(x => x.HeaderControl))
{
if (header is not null)
{
header.SortDirection = null;
}
}
}

internal void ClearFilters()
{
ActiveFilters.Clear();
CollectionView.RefreshFilter();

foreach (var header in Columns.Select(x => x.HeaderControl))
{
if (header is not null)
{
header.IsFiltered = false;
}
}
}

public IAdvancedCollectionView CollectionView { get; private set; } = new AdvancedCollectionView();

internal IDictionary<string, Predicate<object>> ActiveFilters { get; } = new Dictionary<string, Predicate<object>>();
Expand All @@ -369,6 +412,12 @@ public TableViewColumnsCollection Columns
private set => SetValue(ColumnsProperty, value);
}

public double HeaderRowHeight
{
get => (double)GetValue(HeaderRowHeightProperty);
set => SetValue(HeaderRowHeightProperty, value);
}

public double RowHeight
{
get => (double)GetValue(RowHeightProperty);
Expand Down Expand Up @@ -411,14 +460,36 @@ public bool ShowOptionsButton
set => SetValue(ShowOptionsButtonProperty, value);
}

public bool CanResizeColumns
{
get => (bool)GetValue(CanResizeColumnsProperty);
set => SetValue(CanResizeColumnsProperty, value);
}

public bool CanSortColumns
{
get => (bool)GetValue(CanSortColumnsProperty);
set => SetValue(CanSortColumnsProperty, value);
}

public bool CanFilterColumns
{
get => (bool)GetValue(CanFilterColumnsProperty);
set => SetValue(CanFilterColumnsProperty, value);
}

public static readonly new DependencyProperty ItemsSourceProperty = DependencyProperty.Register(nameof(ItemsSource), typeof(IList), typeof(TableView), new PropertyMetadata(null, OnItemsSourceChanged));
public static readonly DependencyProperty ColumnsProperty = DependencyProperty.Register(nameof(Columns), typeof(TableViewColumnsCollection), typeof(TableView), new PropertyMetadata(null));
public static readonly DependencyProperty HeaderRowHeightProperty = DependencyProperty.Register(nameof(HeaderRowHeight), typeof(double), typeof(TableView), new PropertyMetadata(48d));
public static readonly DependencyProperty RowHeightProperty = DependencyProperty.Register(nameof(RowHeight), typeof(double), typeof(TableView), new PropertyMetadata(40d));
public static readonly DependencyProperty RowMaxHeightProperty = DependencyProperty.Register(nameof(RowMaxHeight), typeof(double), typeof(TableView), new PropertyMetadata(double.PositiveInfinity));
public static readonly DependencyProperty ShowExportOptionsProperty = DependencyProperty.Register(nameof(ShowExportOptions), typeof(bool), typeof(TableView), new PropertyMetadata(false));
public static readonly DependencyProperty AutoGenerateColumnsProperty = DependencyProperty.Register(nameof(AutoGenerateColumns), typeof(bool), typeof(TableView), new PropertyMetadata(true, OnAutoGenerateColumnsChanged));
public static readonly DependencyProperty IsReadOnlyProperty = DependencyProperty.Register(nameof(IsReadOnly), typeof(bool), typeof(TableView), new PropertyMetadata(false));
public static readonly DependencyProperty ShowOptionsButtonProperty = DependencyProperty.Register(nameof(ShowOptionsButton), typeof(bool), typeof(TableView), new PropertyMetadata(true));
public static readonly DependencyProperty CanResizeColumnsProperty = DependencyProperty.Register(nameof(CanResizeColumns), typeof(bool), typeof(TableView), new PropertyMetadata(true));
public static readonly DependencyProperty CanSortColumnsProperty = DependencyProperty.Register(nameof(CanSortColumns), typeof(bool), typeof(TableView), new PropertyMetadata(true, OnCanSortColumnsChanged));
public static readonly DependencyProperty CanFilterColumnsProperty = DependencyProperty.Register(nameof(CanFilterColumns), typeof(bool), typeof(TableView), new PropertyMetadata(true, OnCanFilterColumnsChanged));

public event EventHandler<TableViewAutoGeneratingColumnEventArgs>? AutoGeneratingColumn;
public event EventHandler<TableViewExportRowsContentEventArgs>? ExportAllRowsContent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ public OptionsFlyoutViewModel(TableView tableView, TableViewColumnHeader columnH
private void InitializeCommands()
{
SortAscendingCommand.ExecuteRequested += delegate { ColumnHeader.DoSort(SD.Ascending); };
SortAscendingCommand.CanExecuteRequested += (_, e) => e.CanExecute = ColumnHeader._canSort && ColumnHeader.SortDirection != SD.Ascending;
SortAscendingCommand.CanExecuteRequested += (_, e) => e.CanExecute = ColumnHeader.CanSort && ColumnHeader.SortDirection != SD.Ascending;

SortDescendingCommand.ExecuteRequested += delegate { ColumnHeader.DoSort(SD.Descending); };
SortDescendingCommand.CanExecuteRequested += (_, e) => e.CanExecute = ColumnHeader._canSort && ColumnHeader.SortDirection != SD.Descending;
SortDescendingCommand.CanExecuteRequested += (_, e) => e.CanExecute = ColumnHeader.CanSort && ColumnHeader.SortDirection != SD.Descending;

ClearSortingCommand.ExecuteRequested += delegate { ColumnHeader.ClearSorting(); };
ClearSortingCommand.CanExecuteRequested += (_, e) => e.CanExecute = ColumnHeader._canSort && ColumnHeader.SortDirection is not null;
ClearSortingCommand.CanExecuteRequested += (_, e) => e.CanExecute = ColumnHeader.SortDirection is not null;

ClearFilterCommand.ExecuteRequested += delegate { ColumnHeader.ClearFilter(); };
ClearFilterCommand.CanExecuteRequested += (_, e) => e.CanExecute = ColumnHeader.IsFiltered;
Expand Down
32 changes: 19 additions & 13 deletions src/WinUI.TableView/TableViewColumnHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ namespace WinUI.TableView;
[TemplateVisualState(Name = VisualStates.StateUnfiltered, GroupName = VisualStates.GroupFilter)]
public partial class TableViewColumnHeader : ContentControl
{
private bool _canSort;
private bool _canFilter;
private TableView? _tableView;
private TableViewHeaderRow? _headerRow;
private Button? _optionsButton;
Expand All @@ -51,7 +49,7 @@ public TableViewColumnHeader()

private void DoSort(SD direction, bool singleSorting = true)
{
if (_canSort && _tableView is not null)
if (CanSort && _tableView is not null)
{
if (singleSorting)
{
Expand All @@ -78,7 +76,7 @@ private void DoSort(SD direction, bool singleSorting = true)

private void ClearSorting()
{
if (_canSort && _tableView is not null && SortDirection is not null)
if (CanSort && _tableView is not null && SortDirection is not null)
{
SortDirection = null;

Expand Down Expand Up @@ -123,7 +121,7 @@ private bool Filter(object item)

protected override void OnTapped(TappedRoutedEventArgs e)
{
if (_canSort && _tableView is not null && !IsSizingCursor)
if (CanSort && _tableView is not null && !IsSizingCursor)
{
var isCtrlButtonDown = InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.Control) is
CoreVirtualKeyStates.Down or (CoreVirtualKeyStates.Down | CoreVirtualKeyStates.Locked);
Expand All @@ -139,6 +137,7 @@ protected override void OnApplyTemplate()
base.OnApplyTemplate();

_tableView = this.FindAscendant<TableView>();
_tableView?.RegisterPropertyChangedCallback(TableView.CanFilterColumnsProperty, delegate { SetFilterButtonVisibility(); });
_headerRow = this.FindAscendant<TableViewHeaderRow>();
_optionsButton = GetTemplateChild("OptionsButton") as Button;
_optionsFlyout = GetTemplateChild("OptionsFlyout") as MenuFlyout;
Expand All @@ -151,12 +150,10 @@ protected override void OnApplyTemplate()
if (Column is TableViewBoundColumn column && column.Binding.Path.Path is { Length: > 0 } path)
{
_propertyPath = path;
_canSort = column.CanSort;
_canFilter = column.CanFilter;
_optionsButton.Visibility = _canFilter ? Visibility.Visible : Visibility.Collapsed;
column?.RegisterPropertyChangedCallback(TableViewBoundColumn.CanFilterProperty, delegate { SetFilterButtonVisibility(); });
}

if (_canFilter)
if (_optionsButton is not null && _optionsFlyout is not null)
{
_optionsFlyout.Opening += OnOptionsFlyoutOpening;
_optionsButton.Tapped += OnOptionsButtonTaped;
Expand All @@ -176,6 +173,8 @@ protected override void OnApplyTemplate()
searchBox.PreviewKeyDown += OnSearchBoxKeyDown;
}
}

SetFilterButtonVisibility();
}

private void OnSearchBoxKeyDown(object sender, KeyRoutedEventArgs e)
Expand Down Expand Up @@ -282,6 +281,14 @@ private void OnIsFilteredChanged()
}
}

private void SetFilterButtonVisibility()
{
if (_optionsButton is not null)
{
_optionsButton.Visibility = CanFilter ? Visibility.Visible : Visibility.Collapsed;
}
}

private bool IsCursorInRightResizeArea(PointerRoutedEventArgs args)
{
var resizeArea = args.Pointer.PointerDeviceType == PointerDeviceType.Touch ? 8 : 4;
Expand Down Expand Up @@ -385,10 +392,9 @@ internal set

public TableViewColumn? Column { get; internal set; }

private bool CanResize => Column?.CanResize == true;

private bool CanResize => _tableView?.CanResizeColumns == true && Column?.CanResize == true;
private bool CanSort => _tableView?.CanSortColumns == true && Column is TableViewBoundColumn { CanSort: true };
private bool CanFilter => _tableView?.CanFilterColumns == true && Column is TableViewBoundColumn { CanFilter: true };
private bool CanResizePrevious => _headerRow?.GetPreviousHeader(this)?.CanResize == true;

private bool IsSizingCursor => ProtectedCursor is InputSystemCursor { CursorShape: InputSystemCursorShape.SizeWestEast };

}
29 changes: 2 additions & 27 deletions src/WinUI.TableView/TableViewHeaderRow.OptionsFlyoutViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ private void InitializeCommands()
CopyWithHeadersCommand.Description = "Copy the selected row's content including column headers to clipboard.";
CopyWithHeadersCommand.ExecuteRequested += delegate { TableView.CopyToClipboardInternal(true); };

ClearSortingCommand.ExecuteRequested += delegate { ClearSorting(); };
ClearSortingCommand.ExecuteRequested += delegate { TableView.ClearSorting(); };
ClearSortingCommand.CanExecuteRequested += (_, e) => e.CanExecute = TableView.CollectionView.SortDescriptions.Count > 0;

ClearFilterCommand.ExecuteRequested += delegate { ClearFilters(); };
ClearFilterCommand.ExecuteRequested += delegate { TableView.ClearFilters(); };
ClearFilterCommand.CanExecuteRequested += (_, e) => e.CanExecute = TableView.ActiveFilters.Count > 0;

ExportAllToCSVCommand.ExecuteRequested += delegate { TableView.ExportAllToCSV(); };
Expand All @@ -53,32 +53,7 @@ private void InitializeCommands()
ExportSelectedToCSVCommand.CanExecuteRequested += (_, e) => e.CanExecute = TableView.SelectedItems.Count > 0;
}

private void ClearSorting()
{
TableView.CollectionView.SortDescriptions.Clear();

foreach (var header in TableView.Columns.Select(x => x.HeaderControl))
{
if (header is not null)
{
header.SortDirection = null;
}
}
}

private void ClearFilters()
{
TableView.ActiveFilters.Clear();
TableView.CollectionView.RefreshFilter();

foreach (var header in TableView.Columns.Select(x => x.HeaderControl))
{
if (header is not null)
{
header.IsFiltered = false;
}
}
}

public StandardUICommand SelectAllCommand { get; } = new(StandardUICommandKind.SelectAll);
public StandardUICommand DeselectAllCommand { get; } = new() { Label = "Deselect All" };
Expand Down
1 change: 1 addition & 0 deletions src/WinUI.TableView/Themes/TableView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
<ItemsPresenter Padding="{TemplateBinding Padding}">
<ItemsPresenter.Header>
<local:TableViewHeaderRow IsTabStop="False"
Height="{TemplateBinding HeaderRowHeight}"
TableView="{Binding RelativeSource={RelativeSource TemplatedParent}}">
<interactivity:Interaction.Behaviors>
<behaviors:StickyHeaderBehavior />
Expand Down

0 comments on commit 8cd5c27

Please sign in to comment.