Skip to content

Commit

Permalink
Merge branch 'main' into fix/SelectAllCheckBoxIssue
Browse files Browse the repository at this point in the history
  • Loading branch information
w-ahmad authored Dec 12, 2024
2 parents 396270e + edd40a8 commit 1e3678f
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 57 deletions.
8 changes: 4 additions & 4 deletions src/WinUI.TableView/TableView.Properties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions src/WinUI.TableView/TableViewCornerButtonMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace WinUI.TableView;

public enum TableViewCornerButtonMode
{
/// <summary>
/// No button.
/// </summary>
None,

/// <summary>
/// Show Select All button.
/// </summary>
SelectAll,

/// <summary>
/// Show Options button.
/// </summary>
Options
}
59 changes: 41 additions & 18 deletions src/WinUI.TableView/TableViewHeaderRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,6 +28,7 @@ public partial class TableViewHeaderRow : Control
private StackPanel? _headersStackPanel;
private bool _calculatingHeaderWidths;
private DispatcherTimer? _timer;
private readonly Dictionary<DependencyProperty, long> _callbackTokens = new();

public TableViewHeaderRow()
{
Expand All @@ -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);
Expand All @@ -73,9 +69,6 @@ protected override void OnApplyTemplate()
{
_headersStackPanel = stackPanel;
AddHeaders(TableView.Columns.VisibleColumns);

TableView.Columns.CollectionChanged += OnTableViewColumnsCollectionChanged;
TableView.Columns.ColumnPropertyChanged += OnColumnPropertyChanged;
}

SetExportOptionsVisibility();
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(); });
}
}

Expand Down
1 change: 1 addition & 0 deletions src/WinUI.TableView/TableViewRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ private void TableViewRow_Loaded(object sender, RoutedEventArgs e)
_focusVisualMargin = FocusVisualMargin;

EnsureGridLines();
EnsureLayout();
}

protected override void OnApplyTemplate()
Expand Down
65 changes: 32 additions & 33 deletions src/WinUI.TableView/Themes/TableViewHeaderRow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,26 @@
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal" />
</VisualStateGroup>
<VisualStateGroup x:Name="SelectAllButtonStates">
<VisualState x:Name="SelectAllButton" />
<VisualStateGroup x:Name="CornerButtonStates">
<VisualState x:Name="NoButton" />
<VisualState x:Name="SelectAllButton">
<VisualState.Setters>
<Setter Target="selectAllButton.Visibility"
Value="Visible" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="OptionsButton">
<VisualState.Setters>
<Setter Target="optionsButton.Visibility"
Value="Visible" />
<Setter Target="selectAllButton.Visibility"
Value="Collapsed" />
<Setter Target="selectAllCheckBox.Visibility"
Value="Collapsed" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="SelectAllCheckBox">
<VisualState.Setters>
<Setter Target="selectAllCheckBox.Visibility"
Value="Visible" />
<Setter Target="selectAllButton.Visibility"
Value="Collapsed" />
<Setter Target="optionsButton.Visibility"
Value="Collapsed" />
<Setter Target="cornerButtonColumn.Width"
Value="40" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
Expand All @@ -54,43 +54,39 @@
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition x:Name="cornerButtonColumn"
MinWidth="16"
Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>

<Button x:Name="selectAllButton"
VerticalAlignment="Stretch"
BorderThickness="0"
Background="Transparent"
Padding="0"
CornerRadius="4,0,0,0"
Width="16"
Visibility="Visible"
IsTabStop="False">
<FontIcon FontSize="12"
Margin="6,20,0,0"
Opacity="0.5"
Glyph="{ThemeResource SelectAllButtonIcon}"
RenderTransformOrigin="0.5,0.5">
<FontIcon.RenderTransform>
<RotateTransform Angle="-45" />
</FontIcon.RenderTransform>
</FontIcon>
Visibility="Collapsed"
IsTabStop="False"
VerticalAlignment="Stretch">
<FontIcon Opacity="0.5"
Glyph="&#xE788;"
Margin="-8,6,0,0"
VerticalAlignment="Bottom" />
</Button>

<Button x:Name="optionsButton"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
BorderThickness="0"
Background="Transparent"
Padding="0"
CornerRadius="4,0,0,0"
Width="16"
Visibility="Collapsed"
IsTabStop="False">
<FontIcon FontSize="12"
MaxWidth="10"
Margin="-4,0,0,0"
Margin="-2,0,0,0"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Glyph="{ThemeResource OptionsButtonIcon}"
Expand Down Expand Up @@ -151,21 +147,24 @@
</Button>

<CheckBox x:Name="selectAllCheckBox"
Margin="10,0,2,0"
MinWidth="0"
Padding="0"
MinWidth="20"
MinHeight="20"
IsTabStop="False"
Visibility="Collapsed" />
Visibility="Collapsed"
VerticalAlignment="Center"
HorizontalAlignment="Center" />

<Rectangle x:Name="VerticalGridLine"
Grid.Column="1"
Width="1"
VerticalAlignment="Stretch" />
Grid.Column="1"
Width="1"
VerticalAlignment="Stretch" />

<Rectangle x:Name="HorizontalGridLine"
Grid.Row="1"
Grid.ColumnSpan="3"
HorizontalAlignment="Stretch" />

<StackPanel x:Name="HeadersStackPanel"
Grid.Column="2"
Orientation="Horizontal" />
Expand Down
9 changes: 7 additions & 2 deletions src/WinUI.TableView/VisualStates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,12 @@ internal static class VisualStates
/// </summary>
public const string GroupScrollBars = "ScrollBarsStates";

// GroupSelectAllButton
// Group Corner Button

/// <summary>
/// No button state
/// </summary>
public const string StateNoButton = "NoButton";

/// <summary>
/// Select all button state
Expand All @@ -286,7 +291,7 @@ internal static class VisualStates
/// <summary>
/// Select all button state group
/// </summary>
public const string GroupSelectAllButton = "SelectAllButtonStates";
public const string GroupCornerButton = "CornerButtonStates";

/// <summary>
/// Use VisualStateManager to change the visual state of the control.
Expand Down

0 comments on commit 1e3678f

Please sign in to comment.