From cfa2462eef481e5fcfcf71320ee493c2c299b633 Mon Sep 17 00:00:00 2001 From: Waheed Ahmad Date: Wed, 4 Feb 2026 20:34:17 +0500 Subject: [PATCH 01/17] set options menu command in code behind --- src/TableView.Properties.cs | 15 +- src/TableViewHeaderRow.OptionComamnds.cs | 147 +++++++++++++++++ ...bleViewHeaderRow.OptionsFlyoutViewModel.cs | 149 ------------------ src/TableViewHeaderRow.cs | 29 +--- src/Themes/TableViewHeaderRow.xaml | 19 +-- 5 files changed, 169 insertions(+), 190 deletions(-) create mode 100644 src/TableViewHeaderRow.OptionComamnds.cs delete mode 100644 src/TableViewHeaderRow.OptionsFlyoutViewModel.cs diff --git a/src/TableView.Properties.cs b/src/TableView.Properties.cs index 6dbf992d..e9e46f24 100644 --- a/src/TableView.Properties.cs +++ b/src/TableView.Properties.cs @@ -58,7 +58,7 @@ public partial class TableView /// /// Identifies the ShowExportOptions dependency property. /// - public static readonly DependencyProperty ShowExportOptionsProperty = DependencyProperty.Register(nameof(ShowExportOptions), typeof(bool), typeof(TableView), new PropertyMetadata(false)); + public static readonly DependencyProperty ShowExportOptionsProperty = DependencyProperty.Register(nameof(ShowExportOptions), typeof(bool), typeof(TableView), new PropertyMetadata(false, OnShowExportOptionsChanged)); /// /// Identifies the AutoGenerateColumns dependency property. @@ -262,7 +262,7 @@ public partial class TableView /// /// Identifies the dependency property. /// - public static readonly DependencyProperty ShowFilterItemsCountProperty = DependencyProperty.Register(nameof(ShowFilterItemsCount), typeof(bool), typeof(TableView), new PropertyMetadata(false)); + public static readonly DependencyProperty ShowFilterItemsCountProperty = DependencyProperty.Register(nameof(ShowFilterItemsCount), typeof(bool), typeof(TableView), new PropertyMetadata(false)); /// /// Gets or sets a value indicating whether opening the column filter over header right-click is enabled. @@ -820,6 +820,17 @@ private static void OnSelectionModeChanged(DependencyObject d, DependencyPropert } } + /// + /// Handles changes to the ShowExportOptions property. + /// + private static void OnShowExportOptionsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + if (d is TableView tableView) + { + tableView._headerRow?.SetExportOptionsVisibility(); + } + } + /// /// Handles changes to the AutoGenerateColumns property. /// diff --git a/src/TableViewHeaderRow.OptionComamnds.cs b/src/TableViewHeaderRow.OptionComamnds.cs new file mode 100644 index 00000000..76e0c9f6 --- /dev/null +++ b/src/TableViewHeaderRow.OptionComamnds.cs @@ -0,0 +1,147 @@ +using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Controls; +using Microsoft.UI.Xaml.Input; + +namespace WinUI.TableView; + +partial class TableViewHeaderRow +{ + private MenuFlyoutItem? _exportAllMenuItem; + private MenuFlyoutItem? _exportSelectedMenuItem; + private readonly StandardUICommand _selectAllCommand = new(StandardUICommandKind.SelectAll) { Label = TableViewLocalizedStrings.SelectAll }; + private readonly StandardUICommand _deselectAllCommand = new() { Label = TableViewLocalizedStrings.DeselectAll }; + private readonly StandardUICommand _copyCommand = new(StandardUICommandKind.Copy) { Label = TableViewLocalizedStrings.Copy }; + private readonly StandardUICommand _copyWithHeadersCommand = new() { Label = TableViewLocalizedStrings.CopyWithHeaders }; + private readonly StandardUICommand _clearSortingCommand = new() { Label = TableViewLocalizedStrings.ClearSorting }; + private readonly StandardUICommand _clearFilterCommand = new() { Label = TableViewLocalizedStrings.ClearFilter }; + private readonly StandardUICommand _exportAllToCSVCommand = new() { Label = TableViewLocalizedStrings.ExportAll }; + private readonly StandardUICommand _exportSelectedToCSVCommand = new() { Label = TableViewLocalizedStrings.ExportSelected }; + + /// + /// Sets the visibility of the export options. + /// + internal void SetExportOptionsVisibility() + { + var visibility = TableView?.ShowExportOptions is true ? Visibility.Visible : Visibility.Collapsed; + + if (_exportAllMenuItem is not null) + { + _exportAllMenuItem.Visibility = visibility; + } + + if (_exportSelectedMenuItem is not null) + { + _exportSelectedMenuItem.Visibility = visibility; + } + } + + /// + /// Sets commands to option menu items. + /// + private void SetOptionMenuCommands() + { + InitializeCommands(); + + if (GetTemplateChild("SelectAllMenuItem") is MenuFlyoutItem selectAllMenuItem) + selectAllMenuItem.Command = _selectAllCommand; + if (GetTemplateChild("ClearSelectionMenuItem") is MenuFlyoutItem clearSelectionMenuItem) + clearSelectionMenuItem.Command = _deselectAllCommand; + if (GetTemplateChild("CopyMenuItem") is MenuFlyoutItem copyMenuItem) + copyMenuItem.Command = _copyCommand; + if (GetTemplateChild("CopyWithHeadersMenuItem") is MenuFlyoutItem copyWithHeadersMenuItem) + copyWithHeadersMenuItem.Command = _copyWithHeadersCommand; + if (GetTemplateChild("ClearSortingMenuItem") is MenuFlyoutItem clearSortingMenuItem) + clearSortingMenuItem.Command = _clearSortingCommand; + if (GetTemplateChild("ClearFilterMenuItem") is MenuFlyoutItem clearFilterMenuItem) + clearFilterMenuItem.Command = _clearFilterCommand; + if (GetTemplateChild("ExportAllMenuItem") is MenuFlyoutItem exportAllMenuItem) + { + _exportAllMenuItem = exportAllMenuItem; + exportAllMenuItem.Command = _exportAllToCSVCommand; + } + if (GetTemplateChild("ExportSelectedMenuItem") is MenuFlyoutItem exportSelectedMenuItem) + { + _exportSelectedMenuItem = exportSelectedMenuItem; + exportSelectedMenuItem.Command = _exportSelectedToCSVCommand; + } + } + + /// + /// Initializes the commands for the ViewModel. + /// + private void InitializeCommands() + { + _selectAllCommand.Description = TableViewLocalizedStrings.SelectAllCommandDescription; + _selectAllCommand.ExecuteRequested += delegate { TableView?.SelectAll(); }; + _selectAllCommand.CanExecuteRequested += CanExecuteSelectAllCommand; + + _deselectAllCommand.Description = TableViewLocalizedStrings.DeselectAllCommandDescription; + _deselectAllCommand.ExecuteRequested += delegate { TableView?.DeselectAll(); }; + _deselectAllCommand.CanExecuteRequested += CanExecuteDeselectAllCommand; + + _copyCommand.Description = TableViewLocalizedStrings.CopyCommandDescription; + _copyCommand.ExecuteRequested += ExecuteCopyCommand; + _copyCommand.CanExecuteRequested += CanExecuteCopyCommand; + + _copyWithHeadersCommand.Description = TableViewLocalizedStrings.CopyWithHeadersCommandDescription; + _copyWithHeadersCommand.ExecuteRequested += delegate { TableView?.CopyToClipboardInternal(true); }; + _copyWithHeadersCommand.CanExecuteRequested += CanExecuteCopyWithHeadersCommand; + + _clearSortingCommand.ExecuteRequested += delegate { TableView?.ClearAllSortingWithEvent(); }; + _clearSortingCommand.CanExecuteRequested += CanExecuteClearSortingCommand; + + _clearFilterCommand.ExecuteRequested += delegate { TableView?.FilterHandler.ClearFilter(default); }; + _clearFilterCommand.CanExecuteRequested += CanExecuteClearFilterCommand; + + _exportAllToCSVCommand.ExecuteRequested += delegate { TableView?.ExportAllToCSV(); }; + + _exportSelectedToCSVCommand.ExecuteRequested += delegate { TableView?.ExportSelectedToCSV(); }; + _exportSelectedToCSVCommand.CanExecuteRequested += CanExecuteExportSelectedToCSVCommand; + } + + private void CanExecuteSelectAllCommand(XamlUICommand sender, CanExecuteRequestedEventArgs e) + { + e.CanExecute = TableView?.IsEditing is false && TableView.SelectionMode is ListViewSelectionMode.Multiple or ListViewSelectionMode.Extended; + } + + private void CanExecuteDeselectAllCommand(XamlUICommand sender, CanExecuteRequestedEventArgs e) + { + e.CanExecute = TableView?.IsEditing is false && (TableView.SelectedItems.Count > 0 || TableView.SelectedCells.Count > 0); + } + + private void ExecuteCopyCommand(XamlUICommand sender, ExecuteRequestedEventArgs e) + { + var focusedElement = FocusManager.GetFocusedElement(TableView?.XamlRoot!); + if (focusedElement is FrameworkElement { Parent: TableViewCell }) + { + return; + } + + TableView?.CopyToClipboardInternal(false); + } + + private void CanExecuteCopyCommand(XamlUICommand sender, CanExecuteRequestedEventArgs e) + { + e.CanExecute = TableView?.SelectedItems.Count > 0 || TableView?.SelectedCells.Count > 0 || TableView?.CurrentCellSlot.HasValue is true; + } + + private void CanExecuteCopyWithHeadersCommand(XamlUICommand sender, CanExecuteRequestedEventArgs e) + { + e.CanExecute = TableView?.SelectedItems.Count > 0 || TableView?.SelectedCells.Count > 0 || TableView?.CurrentCellSlot.HasValue is true; + } + + private void CanExecuteClearSortingCommand(XamlUICommand sender, CanExecuteRequestedEventArgs e) + { + e.CanExecute = TableView?.IsEditing is false && TableView.IsSorted; + } + + private void CanExecuteClearFilterCommand(XamlUICommand sender, CanExecuteRequestedEventArgs e) + { + e.CanExecute = TableView?.IsEditing is false && TableView.IsFiltered; + } + + private void CanExecuteExportSelectedToCSVCommand(XamlUICommand sender, CanExecuteRequestedEventArgs e) + { + e.CanExecute = TableView?.IsEditing is false && (TableView.SelectedItems.Count > 0 || TableView.SelectedCells.Count > 0 || TableView.CurrentCellSlot.HasValue); + } +} diff --git a/src/TableViewHeaderRow.OptionsFlyoutViewModel.cs b/src/TableViewHeaderRow.OptionsFlyoutViewModel.cs deleted file mode 100644 index 26124c09..00000000 --- a/src/TableViewHeaderRow.OptionsFlyoutViewModel.cs +++ /dev/null @@ -1,149 +0,0 @@ -using Microsoft.UI.Xaml; -using Microsoft.UI.Xaml.Controls; -using Microsoft.UI.Xaml.Input; -using System; - -namespace WinUI.TableView; - -public partial class TableViewHeaderRow -{ - /// - /// ViewModel for the options flyout in the TableViewHeaderRow. - /// - private class OptionsFlyoutViewModel - { - /// - /// Initializes a new instance of the OptionsFlyoutViewModel class. - /// - /// The TableView associated with the ViewModel. - public OptionsFlyoutViewModel(TableView _tableView) - { - InitializeCommands(); - TableView = _tableView; - } - - /// - /// Initializes the commands for the ViewModel. - /// - private void InitializeCommands() - { - SelectAllCommand.Description = TableViewLocalizedStrings.SelectAllCommandDescription; - SelectAllCommand.ExecuteRequested += delegate { TableView.SelectAll(); }; - SelectAllCommand.CanExecuteRequested += CanExecuteSelectAllCommand; - - DeselectAllCommand.Description = TableViewLocalizedStrings.DeselectAllCommandDescription; - DeselectAllCommand.ExecuteRequested += delegate { TableView.DeselectAll(); }; - DeselectAllCommand.CanExecuteRequested += CanExecuteDeselectAllCommand; - - CopyCommand.Description = TableViewLocalizedStrings.CopyCommandDescription; - CopyCommand.ExecuteRequested += ExecuteCopyCommand; - CopyCommand.CanExecuteRequested += CanExecuteCopyCommand; - - CopyWithHeadersCommand.Description = TableViewLocalizedStrings.CopyWithHeadersCommandDescription; - CopyWithHeadersCommand.ExecuteRequested += delegate { TableView.CopyToClipboardInternal(true); }; - CopyWithHeadersCommand.CanExecuteRequested += CanExecuteCopyWithHeadersCommand; - - ClearSortingCommand.ExecuteRequested += delegate { TableView.ClearAllSortingWithEvent(); }; - ClearSortingCommand.CanExecuteRequested += CanExecuteClearSortingCommand; - - ClearFilterCommand.ExecuteRequested += delegate { TableView.FilterHandler.ClearFilter(default); }; - ClearFilterCommand.CanExecuteRequested += CanExecuteClearFilterCommand; - - ExportAllToCSVCommand.ExecuteRequested += delegate { TableView.ExportAllToCSV(); }; - - ExportSelectedToCSVCommand.ExecuteRequested += delegate { TableView.ExportSelectedToCSV(); }; - ExportSelectedToCSVCommand.CanExecuteRequested += CanExecuteExportSelectedToCSVCommand; - } - - private void CanExecuteSelectAllCommand(XamlUICommand sender, CanExecuteRequestedEventArgs e) - { - e.CanExecute = !TableView.IsEditing && TableView.SelectionMode is ListViewSelectionMode.Multiple or ListViewSelectionMode.Extended; - } - - private void CanExecuteDeselectAllCommand(XamlUICommand sender, CanExecuteRequestedEventArgs e) - { - e.CanExecute = !TableView.IsEditing && (TableView.SelectedItems.Count > 0 || TableView.SelectedCells.Count > 0); - } - - private void ExecuteCopyCommand(XamlUICommand sender, ExecuteRequestedEventArgs e) - { - var focusedElement = FocusManager.GetFocusedElement(TableView.XamlRoot!); - if (focusedElement is FrameworkElement { Parent: TableViewCell }) - { - return; - } - - TableView.CopyToClipboardInternal(false); - } - - private void CanExecuteCopyCommand(XamlUICommand sender, CanExecuteRequestedEventArgs e) - { - e.CanExecute = TableView.SelectedItems.Count > 0 || TableView.SelectedCells.Count > 0 || TableView.CurrentCellSlot.HasValue; - } - - private void CanExecuteCopyWithHeadersCommand(XamlUICommand sender, CanExecuteRequestedEventArgs e) - { - e.CanExecute = TableView.SelectedItems.Count > 0 || TableView.SelectedCells.Count > 0 || TableView.CurrentCellSlot.HasValue; - } - - private void CanExecuteClearSortingCommand(XamlUICommand sender, CanExecuteRequestedEventArgs e) - { - e.CanExecute = !TableView.IsEditing && TableView.IsSorted; - } - - private void CanExecuteClearFilterCommand(XamlUICommand sender, CanExecuteRequestedEventArgs e) - { - e.CanExecute = !TableView.IsEditing && TableView.IsFiltered; - } - - private void CanExecuteExportSelectedToCSVCommand(XamlUICommand sender, CanExecuteRequestedEventArgs e) - { - e.CanExecute = !TableView.IsEditing && (TableView.SelectedItems.Count > 0 || TableView.SelectedCells.Count > 0 || TableView.CurrentCellSlot.HasValue); - } - - /// - /// Gets the command to select all rows. - /// - public StandardUICommand SelectAllCommand { get; } = new(StandardUICommandKind.SelectAll) { Label = TableViewLocalizedStrings.SelectAll }; - - /// - /// Gets the command to deselect all rows. - /// - public StandardUICommand DeselectAllCommand { get; } = new() { Label = TableViewLocalizedStrings.DeselectAll }; - - /// - /// Gets the command to copy the selected row's content to the clipboard. - /// - public StandardUICommand CopyCommand { get; } = new(StandardUICommandKind.Copy) { Label = TableViewLocalizedStrings.Copy }; - - /// - /// Gets the command to copy the selected row's content including column headers to the clipboard. - /// - public StandardUICommand CopyWithHeadersCommand { get; } = new() { Label = TableViewLocalizedStrings.CopyWithHeaders }; - - /// - /// Gets the command to clear sorting. - /// - public StandardUICommand ClearSortingCommand { get; } = new() { Label = TableViewLocalizedStrings.ClearSorting }; - - /// - /// Gets the command to clear filters. - /// - public StandardUICommand ClearFilterCommand { get; } = new() { Label = TableViewLocalizedStrings.ClearFilter }; - - /// - /// Gets the command to export all content to a CSV file. - /// - public StandardUICommand ExportAllToCSVCommand { get; } = new() { Label = TableViewLocalizedStrings.ExportAll }; - - /// - /// Gets the command to export selected content to a CSV file. - /// - public StandardUICommand ExportSelectedToCSVCommand { get; } = new() { Label = TableViewLocalizedStrings.ExportSelected }; - - /// - /// Gets the TableView associated with the ViewModel. - /// - public TableView TableView { get; } - } -} diff --git a/src/TableViewHeaderRow.cs b/src/TableViewHeaderRow.cs index 1224f92b..0a6bb040 100644 --- a/src/TableViewHeaderRow.cs +++ b/src/TableViewHeaderRow.cs @@ -70,11 +70,6 @@ protected override void OnApplyTemplate() return; } - if (_optionsButton is not null) - { - _optionsButton.DataContext = new OptionsFlyoutViewModel(TableView); - } - if (GetTemplateChild("selectAllButton") is Button selectAllButton) { selectAllButton.Tapped += OnSelectAllButtonClicked; @@ -97,6 +92,7 @@ protected override void OnApplyTemplate() Path = new PropertyPath(nameof(TableView.CellsHorizontalOffset)) }); + SetOptionMenuCommands(); SetExportOptionsVisibility(); SetCornerButtonState(); SetHeadersVisibility(); @@ -364,29 +360,6 @@ internal void CalculateHeaderWidths() } } - /// - /// Sets the visibility of the export options. - /// - private void SetExportOptionsVisibility() - { - var binding = new Binding - { - Path = new PropertyPath(nameof(TableView.ShowExportOptions)), - Source = TableView, - Converter = new BoolToVisibilityConverter() - }; - - if (GetTemplateChild("ExportAllMenuItem") is MenuFlyoutItem exportAll) - { - exportAll.SetBinding(VisibilityProperty, binding); - } - - if (GetTemplateChild("ExportSelectedMenuItem") is MenuFlyoutItem exportSelected) - { - exportSelected.SetBinding(VisibilityProperty, binding); - } - } - /// /// Handles the selection changed event for the TableView. /// diff --git a/src/Themes/TableViewHeaderRow.xaml b/src/Themes/TableViewHeaderRow.xaml index f68da69b..577a25d0 100644 --- a/src/Themes/TableViewHeaderRow.xaml +++ b/src/Themes/TableViewHeaderRow.xaml @@ -81,7 +81,6 @@ - public int Count { get; set; } - /// public override string ToString() { From 44fcc4211022315c3c3ecd190e96f6c4e4b00698 Mon Sep 17 00:00:00 2001 From: Waheed Ahmad Date: Fri, 6 Feb 2026 22:01:43 +0500 Subject: [PATCH 03/17] set filter flyout commands in code behind --- src/TableViewColumnHeader.OptionComamnds.cs | 70 +++++++++++++++++++++ src/Themes/TableViewColumnHeader.xaml | 14 ++--- 2 files changed, 77 insertions(+), 7 deletions(-) create mode 100644 src/TableViewColumnHeader.OptionComamnds.cs diff --git a/src/TableViewColumnHeader.OptionComamnds.cs b/src/TableViewColumnHeader.OptionComamnds.cs new file mode 100644 index 00000000..a90976e9 --- /dev/null +++ b/src/TableViewColumnHeader.OptionComamnds.cs @@ -0,0 +1,70 @@ +using Microsoft.UI.Xaml.Controls; +using Microsoft.UI.Xaml.Input; +using WinUI.TableView.Extensions; +using SD = WinUI.TableView.SortDirection; + +namespace WinUI.TableView; + +partial class TableViewColumnHeader +{ + private readonly StandardUICommand _sortAscendingCommand = new() { Label = TableViewLocalizedStrings.SortAscending }; + private readonly StandardUICommand _sortDescendingCommand = new() { Label = TableViewLocalizedStrings.SortDescending }; + private readonly StandardUICommand _clearSortingCommand = new() { Label = TableViewLocalizedStrings.ClearSorting }; + private readonly StandardUICommand _clearFilterCommand = new() { Label = TableViewLocalizedStrings.ClearFilter }; + private readonly StandardUICommand _okCommand = new() { Label = TableViewLocalizedStrings.Ok }; + private readonly StandardUICommand _cancelCommand = new() { Label = TableViewLocalizedStrings.Cancel }; + + /// + /// Sets commands to option menu items. + /// + private void SetOptionCommands() + { + InitializeCommands(); + + if (GetTemplateChild("SortAscendingMenuItem") is MenuFlyoutItem sortAscendingMenuItem) + sortAscendingMenuItem.Command = _sortAscendingCommand; + if (GetTemplateChild("SortDescendingMenuItem") is MenuFlyoutItem sortDescendingMenuItem) + sortDescendingMenuItem.Command = _sortDescendingCommand; + if (GetTemplateChild("ClearSortingMenuItem") is MenuFlyoutItem clearSortingMenuItem) + clearSortingMenuItem.Command = _clearSortingCommand; + if (GetTemplateChild("ClearFilterMenuItem") is MenuFlyoutItem clearFilterMenuItem) + clearFilterMenuItem.Command = _clearFilterCommand; + if (GetTemplateChild("ActionButtonsMenuItem") is MenuFlyoutItem actionButtonsMenuItem) + { + actionButtonsMenuItem.ApplyTemplate(); + + if (actionButtonsMenuItem.FindDescendant