From 9eaec55bf8dc79324ab53e9ffdf54409c13b20cc Mon Sep 17 00:00:00 2001 From: Waheed Ahmad Date: Tue, 10 Dec 2024 20:46:07 +0500 Subject: [PATCH 1/3] replaced ShowOptionsButton proeprty with CornerButtonMode --- src/WinUI.TableView/TableView.Properties.cs | 8 +-- .../TableViewCornerButtonMode.cs | 19 ++++++ src/WinUI.TableView/TableViewHeaderRow.cs | 51 ++++++++++---- src/WinUI.TableView/TableViewRow.cs | 1 + .../Themes/TableViewHeaderRow.xaml | 66 +++++++++---------- src/WinUI.TableView/VisualStates.cs | 9 ++- 6 files changed, 103 insertions(+), 51 deletions(-) create mode 100644 src/WinUI.TableView/TableViewCornerButtonMode.cs diff --git a/src/WinUI.TableView/TableView.Properties.cs b/src/WinUI.TableView/TableView.Properties.cs index af3ca91..f2b1b54 100644 --- a/src/WinUI.TableView/TableView.Properties.cs +++ b/src/WinUI.TableView/TableView.Properties.cs @@ -17,7 +17,7 @@ public partial class TableView 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, OnIsReadOnlyChanged)); - public static readonly DependencyProperty ShowOptionsButtonProperty = DependencyProperty.Register(nameof(ShowOptionsButton), typeof(bool), typeof(TableView), new PropertyMetadata(true)); + public static readonly DependencyProperty CornerButtonModeProperty = DependencyProperty.Register(nameof(CornerButtonMode), typeof(TableViewCornerButtonMode), typeof(TableView), new PropertyMetadata(TableViewCornerButtonMode.Options)); 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)); @@ -91,10 +91,10 @@ public bool IsReadOnly set => SetValue(IsReadOnlyProperty, value); } - public bool ShowOptionsButton + public TableViewCornerButtonMode CornerButtonMode { - get => (bool)GetValue(ShowOptionsButtonProperty); - set => SetValue(ShowOptionsButtonProperty, value); + get => (TableViewCornerButtonMode)GetValue(CornerButtonModeProperty); + set => SetValue(CornerButtonModeProperty, value); } public bool CanResizeColumns diff --git a/src/WinUI.TableView/TableViewCornerButtonMode.cs b/src/WinUI.TableView/TableViewCornerButtonMode.cs new file mode 100644 index 0000000..c85a72c --- /dev/null +++ b/src/WinUI.TableView/TableViewCornerButtonMode.cs @@ -0,0 +1,19 @@ +namespace WinUI.TableView; + +public enum TableViewCornerButtonMode +{ + /// + /// No button. + /// + None, + + /// + /// Show Select All button. + /// + SelectAll, + + /// + /// Show Options button. + /// + Options +} \ No newline at end of file diff --git a/src/WinUI.TableView/TableViewHeaderRow.cs b/src/WinUI.TableView/TableViewHeaderRow.cs index 918f57e..412b439 100644 --- a/src/WinUI.TableView/TableViewHeaderRow.cs +++ b/src/WinUI.TableView/TableViewHeaderRow.cs @@ -15,9 +15,10 @@ namespace WinUI.TableView; [TemplateVisualState(Name = VisualStates.StateNormal, GroupName = VisualStates.GroupCommon)] -[TemplateVisualState(Name = VisualStates.StateSelectAllButton, GroupName = VisualStates.GroupSelectAllButton)] -[TemplateVisualState(Name = VisualStates.StateSelectAllCheckBox, GroupName = VisualStates.GroupSelectAllButton)] -[TemplateVisualState(Name = VisualStates.StateOptionsButton, GroupName = VisualStates.GroupSelectAllButton)] +[TemplateVisualState(Name = VisualStates.StateNoButton, GroupName = VisualStates.GroupCornerButton)] +[TemplateVisualState(Name = VisualStates.StateSelectAllButton, GroupName = VisualStates.GroupCornerButton)] +[TemplateVisualState(Name = VisualStates.StateSelectAllCheckBox, GroupName = VisualStates.GroupCornerButton)] +[TemplateVisualState(Name = VisualStates.StateOptionsButton, GroupName = VisualStates.GroupCornerButton)] public partial class TableViewHeaderRow : Control { private Button? _optionsButton; @@ -27,6 +28,7 @@ public partial class TableViewHeaderRow : Control private StackPanel? _headersStackPanel; private bool _calculatingHeaderWidths; private DispatcherTimer? _timer; + private readonly Dictionary _callbackTokens = new(); public TableViewHeaderRow() { @@ -72,9 +74,6 @@ protected override void OnApplyTemplate() { _headersStackPanel = stackPanel; AddHeaders(TableView.Columns.VisibleColumns); - - TableView.Columns.CollectionChanged += OnTableViewColumnsCollectionChanged; - TableView.Columns.ColumnPropertyChanged += OnColumnPropertyChanged; } SetExportOptionsVisibility(); @@ -333,14 +332,18 @@ private void SetSelectAllButtonState() { VisualStates.GoToState(this, false, VisualStates.StateSelectAllCheckBox); } - else if (TableView?.ShowOptionsButton is true) + else if (TableView?.CornerButtonMode is TableViewCornerButtonMode.Options) { VisualStates.GoToState(this, false, VisualStates.StateOptionsButton); } - else + else if (TableView?.CornerButtonMode is TableViewCornerButtonMode.SelectAll) { VisualStates.GoToState(this, false, VisualStates.StateSelectAllButton); } + else + { + VisualStates.GoToState(this, false, VisualStates.StateNoButton); + } } private void OnSelectAllCheckBoxTapped(object sender, RoutedEventArgs e) @@ -406,15 +409,41 @@ private static void OnTableViewChanged(DependencyObject d, DependencyPropertyCha { if (d is TableViewHeaderRow headerRow) { + headerRow.OnTableViewChanged(e); + } + } + + private void OnTableViewChanged(DependencyPropertyChangedEventArgs e) + { if (e.OldValue is TableView oldTableView) { - oldTableView.SizeChanged -= headerRow.OnTableViewSizeChanged; + oldTableView.SizeChanged -= OnTableViewSizeChanged; + oldTableView.SelectionChanged -= delegate { OnTableViewSelectionChanged(); }; + oldTableView.Items.VectorChanged -= delegate { OnTableViewSelectionChanged(); }; + oldTableView.Columns.CollectionChanged -= OnTableViewColumnsCollectionChanged; + oldTableView.Columns.ColumnPropertyChanged -= OnColumnPropertyChanged; + + oldTableView.UnregisterPropertyChangedCallback(ListViewBase.SelectionModeProperty, _callbackTokens[ListViewBase.SelectionModeProperty]); + oldTableView.UnregisterPropertyChangedCallback(TableView.CornerButtonModeProperty, _callbackTokens[TableView.CornerButtonModeProperty]); + oldTableView.UnregisterPropertyChangedCallback(TableView.ItemsSourceProperty, _callbackTokens[TableView.ItemsSourceProperty]); } if (e.NewValue is TableView newTableView) { - newTableView.SizeChanged += headerRow.OnTableViewSizeChanged; - } + newTableView.SizeChanged += OnTableViewSizeChanged; + newTableView.SelectionChanged += delegate { OnTableViewSelectionChanged(); }; + newTableView.Items.VectorChanged += delegate { OnTableViewSelectionChanged(); }; + newTableView.Columns.CollectionChanged += OnTableViewColumnsCollectionChanged; + newTableView.Columns.ColumnPropertyChanged += OnColumnPropertyChanged; + + _callbackTokens[ListViewBase.SelectionModeProperty] = + newTableView.RegisterPropertyChangedCallback(ListViewBase.SelectionModeProperty, delegate { SetSelectAllButtonState(); }); + + _callbackTokens[TableView.CornerButtonModeProperty] = + newTableView.RegisterPropertyChangedCallback(TableView.CornerButtonModeProperty, delegate { SetSelectAllButtonState(); }); + + _callbackTokens[TableView.ItemsSourceProperty] = + newTableView.RegisterPropertyChangedCallback(TableView.ItemsSourceProperty, delegate { OnTableViewSelectionChanged(); }); } } diff --git a/src/WinUI.TableView/TableViewRow.cs b/src/WinUI.TableView/TableViewRow.cs index f839887..9e12655 100644 --- a/src/WinUI.TableView/TableViewRow.cs +++ b/src/WinUI.TableView/TableViewRow.cs @@ -65,6 +65,7 @@ private void TableViewRow_Loaded(object sender, RoutedEventArgs e) _focusVisualMargin = FocusVisualMargin; EnsureGridLines(); + EnsureLayout(); } protected override void OnApplyTemplate() diff --git a/src/WinUI.TableView/Themes/TableViewHeaderRow.xaml b/src/WinUI.TableView/Themes/TableViewHeaderRow.xaml index d373fa6..8ba38e3 100644 --- a/src/WinUI.TableView/Themes/TableViewHeaderRow.xaml +++ b/src/WinUI.TableView/Themes/TableViewHeaderRow.xaml @@ -20,26 +20,26 @@ - - + + + + + + + - - - - + @@ -54,43 +54,39 @@ - +