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 6cf0c43..c2ce6ef 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() { @@ -41,18 +43,12 @@ protected override void OnApplyTemplate() _selectAllCheckBox = GetTemplateChild("selectAllCheckBox") as CheckBox; _v_gridLine = GetTemplateChild("VerticalGridLine") as Rectangle; _h_gridLine = GetTemplateChild("HorizontalGridLine") as Rectangle; - TableView?.RegisterPropertyChangedCallback(ListViewBase.SelectionModeProperty, delegate { SetSelectAllButtonState(); }); - TableView?.RegisterPropertyChangedCallback(TableView.ShowOptionsButtonProperty, delegate { SetSelectAllButtonState(); }); - TableView?.RegisterPropertyChangedCallback(TableView.ItemsSourceProperty, delegate { OnTableViewSelectionChanged(); }); if (TableView is null) { return; } - TableView.SelectionChanged += delegate { OnTableViewSelectionChanged(); }; - TableView.Items.VectorChanged += delegate { OnTableViewSelectionChanged(); }; - if (_optionsButton is not null) { _optionsButton.DataContext = new OptionsFlyoutViewModel(TableView); @@ -73,9 +69,6 @@ protected override void OnApplyTemplate() { _headersStackPanel = stackPanel; AddHeaders(TableView.Columns.VisibleColumns); - - TableView.Columns.CollectionChanged += OnTableViewColumnsCollectionChanged; - TableView.Columns.ColumnPropertyChanged += OnColumnPropertyChanged; } SetExportOptionsVisibility(); @@ -330,18 +323,22 @@ private void OnTableViewSelectionChanged() private void SetSelectAllButtonState() { - if (TableView?.SelectionMode == ListViewSelectionMode.Multiple) + if (TableView is ListView { SelectionMode: ListViewSelectionMode.Multiple }) { VisualStates.GoToState(this, false, VisualStates.StateSelectAllCheckBox); } - else if (TableView?.ShowOptionsButton is true) + else if (TableView is { CornerButtonMode: TableViewCornerButtonMode.Options }) { VisualStates.GoToState(this, false, VisualStates.StateOptionsButton); } - else + else if (TableView is { CornerButtonMode: TableViewCornerButtonMode.SelectAll }) { VisualStates.GoToState(this, false, VisualStates.StateSelectAllButton); } + else + { + VisualStates.GoToState(this, false, VisualStates.StateNoButton); + } } private void OnSelectAllCheckBoxChecked(object sender, RoutedEventArgs e) @@ -402,15 +399,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 6fd5498..8ba38e3 100644 --- a/src/WinUI.TableView/Themes/TableViewHeaderRow.xaml +++ b/src/WinUI.TableView/Themes/TableViewHeaderRow.xaml @@ -20,26 +20,26 @@ - - + + + + + + + - - - - + @@ -54,43 +54,39 @@ - +