Skip to content

Commit b745a30

Browse files
authored
Merge pull request #131 from w-ahmad/SelectAllFix
fixed select all button action
2 parents e2db713 + d217170 commit b745a30

File tree

7 files changed

+129
-36
lines changed

7 files changed

+129
-36
lines changed

src/TableView.Properties.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public partial class TableView
6363
/// <summary>
6464
/// Identifies the CornerButtonMode dependency property.
6565
/// </summary>
66-
public static readonly DependencyProperty CornerButtonModeProperty = DependencyProperty.Register(nameof(CornerButtonMode), typeof(TableViewCornerButtonMode), typeof(TableView), new PropertyMetadata(TableViewCornerButtonMode.Options));
66+
public static readonly DependencyProperty CornerButtonModeProperty = DependencyProperty.Register(nameof(CornerButtonMode), typeof(TableViewCornerButtonMode), typeof(TableView), new PropertyMetadata(TableViewCornerButtonMode.Options, OnCornerButtonModeChanged));
6767

6868
/// <summary>
6969
/// Identifies the CanResizeColumns dependency property.
@@ -216,6 +216,9 @@ public partial class TableView
216216
/// </summary>
217217
internal int SelectionIndicatorWidth => SelectionMode is ListViewSelectionMode.Multiple ? 44 : 16;
218218

219+
/// <summary>
220+
/// Gets or sets the filter handler for the TableView.
221+
/// </summary>
219222
public IColumnFilterHandler FilterHandler { get; set; }
220223

221224
/// <summary>
@@ -522,6 +525,7 @@ private static void OnSelectionModeChanged(DependencyObject d, DependencyPropert
522525
}
523526

524527
tableView.UpdateBaseSelectionMode();
528+
tableView.UpdateCornerButtonState();
525529
}
526530
}
527531

@@ -551,6 +555,17 @@ private static void OnAutoGenerateColumnsChanged(DependencyObject d, DependencyP
551555
}
552556
}
553557

558+
/// <summary>
559+
/// Handles changes to the CornerButtonMode property.
560+
/// </summary>
561+
private static void OnCornerButtonModeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
562+
{
563+
if (d is TableView tableView)
564+
{
565+
tableView.UpdateCornerButtonState();
566+
}
567+
}
568+
554569
/// <summary>
555570
/// Handles changes to the IsReadOnly property.
556571
/// </summary>
@@ -619,6 +634,7 @@ private static void OnSelectionUnitChanged(DependencyObject d, DependencyPropert
619634
}
620635

621636
tableView.UpdateBaseSelectionMode();
637+
tableView.UpdateCornerButtonState();
622638
}
623639
}
624640

src/TableView.cs

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ private void HandleNavigations(KeyRoutedEventArgs e, bool shiftKey, bool ctrlKey
150150
if (e.Key is VirtualKey.F2 && currentCell is not null && !IsEditing)
151151
{
152152
currentCell.PrepareForEdit();
153-
IsEditing = true;
154153
e.Handled = true;
155154
}
156155
else if (e.Key is VirtualKey.Escape && currentCell is not null && IsEditing)
@@ -367,7 +366,7 @@ public string GetSelectedContent(bool includeHeaders, char separator = '\t')
367366
.Select(c => new TableViewCellSlot(r, c)))
368367
.Concat(SelectedCells)
369368
.OrderBy(x => x.Row)
370-
.ThenByDescending(x => x.Column);
369+
.ThenByDescending(x => x.Column);
371370
#endif
372371
}
373372
else if (CurrentCellSlot.HasValue)
@@ -675,7 +674,7 @@ internal async void ExportAllToCSV()
675674
using var tw = new StreamWriter(stream);
676675
await tw.WriteAsync(content);
677676
}
678-
catch { }
677+
catch { }
679678
#else
680679
await Task.CompletedTask;
681680
#endif
@@ -815,7 +814,7 @@ public void RefreshFilter()
815814
case ListViewSelectionMode.Multiple:
816815
case ListViewSelectionMode.Extended:
817816
#if WINDOWS
818-
SelectRange(new ItemIndexRange(0, (uint)Items.Count));
817+
SelectRange(new ItemIndexRange(0, (uint)Items.Count));
819818
#endif
820819
break;
821820
}
@@ -877,7 +876,7 @@ private void DeselectAllItems()
877876
case ListViewSelectionMode.Multiple:
878877
case ListViewSelectionMode.Extended:
879878
#if WINDOWS
880-
DeselectRange(new ItemIndexRange(0, (uint)Items.Count));
879+
DeselectRange(new ItemIndexRange(0, (uint)Items.Count));
881880
#endif
882881
break;
883882
}
@@ -986,7 +985,7 @@ private void SelectRows(TableViewCellSlot slot, bool shiftKey)
986985
var row = await ScrollRowIntoView(slot.Row);
987986
row?.Focus(FocusState.Programmatic);
988987
});
989-
}
988+
}
990989
#endif
991990
}
992991

@@ -1309,7 +1308,7 @@ internal void EnsureAlternateRowColors()
13091308
{
13101309
row.EnsureAlternateColors();
13111310
}
1312-
});
1311+
});
13131312
}
13141313

13151314
/// <summary>
@@ -1341,7 +1340,7 @@ internal void EnsureCells()
13411340
{
13421341
row.EnsureCells();
13431342
}
1344-
}
1343+
}
13451344
#endif
13461345

13471346
#if WINDOWS
@@ -1359,7 +1358,7 @@ internal void ShowRowContext(TableViewRow row, Point position)
13591358

13601359
RowContextFlyout.ShowAt(presenter, new FlyoutShowOptions
13611360
{
1362-
ShowMode = FlyoutShowMode.Standard,
1361+
ShowMode = FlyoutShowMode.Standard,
13631362
Placement = RowContextFlyout.Placement,
13641363
Position = position
13651364
});
@@ -1378,14 +1377,33 @@ internal void ShowCellContext(TableViewCell cell, Point position)
13781377
{
13791378
CellContextFlyout.ShowAt(cell, new FlyoutShowOptions
13801379
{
1381-
ShowMode = FlyoutShowMode.Standard,
1380+
ShowMode = FlyoutShowMode.Standard,
13821381
Placement = CellContextFlyout.Placement,
13831382
Position = position
13841383
});
13851384
}
13861385
}
13871386
#endif
13881387

1388+
/// <summary>
1389+
/// Sets the state of the corner button.
1390+
/// </summary>
1391+
internal void UpdateCornerButtonState()
1392+
{
1393+
_headerRow?.SetCornerButtonState();
1394+
1395+
DispatcherQueue.TryEnqueue(Microsoft.UI.Dispatching.DispatcherQueuePriority.Low,() =>
1396+
{
1397+
if (SelectionMode is ListViewSelectionMode.Multiple && SelectionUnit is not TableViewSelectionUnit.Cell)
1398+
{
1399+
foreach (var row in _rows)
1400+
{
1401+
row.UpdateSelectCheckMarkOpacity();
1402+
}
1403+
}
1404+
});
1405+
}
1406+
13891407
public virtual void OnSorting(TableViewSortingEventArgs eventArgs)
13901408
{
13911409
Sorting?.Invoke(this, eventArgs);
@@ -1434,7 +1452,7 @@ public virtual void OnClearSorting(TableViewClearSortingEventArgs eventArgs)
14341452
/// <summary>
14351453
/// Event triggered when the cell context flyout is opening.
14361454
/// </summary>
1437-
public event EventHandler<TableViewCellContextFlyoutEventArgs>? CellContextFlyoutOpening;
1455+
public event EventHandler<TableViewCellContextFlyoutEventArgs>? CellContextFlyoutOpening;
14381456
#endif
14391457

14401458
/// <summary>

src/TableViewCell.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,6 @@ protected override void OnDoubleTapped(DoubleTappedRoutedEventArgs e)
252252
if (!IsReadOnly && TableView is not null && !TableView.IsEditing && !Column?.UseSingleElement is true)
253253
{
254254
PrepareForEdit();
255-
256-
TableView.IsEditing = true;
257255
}
258256
}
259257

@@ -287,6 +285,7 @@ private void MakeSelection()
287285
}
288286

289287
TableView.IsEditing = false;
288+
TableView.UpdateCornerButtonState();
290289
TableView.MakeSelection(Slot, shiftKey, ctrlKey);
291290
}
292291
}
@@ -341,6 +340,7 @@ private void SetEditingElement()
341340
if (TableView is not null)
342341
{
343342
TableView.IsEditing = true;
343+
TableView.UpdateCornerButtonState();
344344
}
345345
}
346346

src/TableViewHeaderRow.cs

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
using CommunityToolkit.WinUI;
21
using Microsoft.UI;
32
using Microsoft.UI.Xaml;
43
using Microsoft.UI.Xaml.Controls;
54
using Microsoft.UI.Xaml.Data;
5+
using Microsoft.UI.Xaml.Input;
66
using Microsoft.UI.Xaml.Media;
77
using Microsoft.UI.Xaml.Shapes;
88
using System;
@@ -20,8 +20,11 @@ namespace WinUI.TableView;
2020
[TemplateVisualState(Name = VisualStates.StateNormal, GroupName = VisualStates.GroupCommon)]
2121
[TemplateVisualState(Name = VisualStates.StateNoButton, GroupName = VisualStates.GroupCornerButton)]
2222
[TemplateVisualState(Name = VisualStates.StateSelectAllButton, GroupName = VisualStates.GroupCornerButton)]
23+
[TemplateVisualState(Name = VisualStates.StateSelectAllButtonDisabled, GroupName = VisualStates.GroupCornerButton)]
2324
[TemplateVisualState(Name = VisualStates.StateSelectAllCheckBox, GroupName = VisualStates.GroupCornerButton)]
25+
[TemplateVisualState(Name = VisualStates.StateSelectAllCheckBoxDisabled, GroupName = VisualStates.GroupCornerButton)]
2426
[TemplateVisualState(Name = VisualStates.StateOptionsButton, GroupName = VisualStates.GroupCornerButton)]
27+
[TemplateVisualState(Name = VisualStates.StateOptionsButtonDisabled, GroupName = VisualStates.GroupCornerButton)]
2528
public partial class TableViewHeaderRow : Control
2629
{
2730
private Button? _optionsButton;
@@ -62,7 +65,7 @@ protected override void OnApplyTemplate()
6265

6366
if (GetTemplateChild("selectAllButton") is Button selectAllButton)
6467
{
65-
selectAllButton.Tapped += delegate { TableView.SelectAllSafe(); };
68+
selectAllButton.Tapped += OnSelectAllButtonClicked;
6669
}
6770

6871
if (_selectAllCheckBox is not null)
@@ -81,11 +84,11 @@ protected override void OnApplyTemplate()
8184
if (GetTemplateChild("cornerButtonColumn") is ColumnDefinition cornerButtonColumn)
8285
{
8386
cornerButtonColumn.MinWidth = 20;
84-
}
87+
}
8588
#endif
8689

8790
SetExportOptionsVisibility();
88-
SetSelectAllButtonState();
91+
SetCornerButtonState();
8992
EnsureGridLines();
9093
}
9194

@@ -355,32 +358,40 @@ private void OnTableViewSelectionChanged()
355358
/// <summary>
356359
/// Sets the state of the select all button.
357360
/// </summary>
358-
private void SetSelectAllButtonState()
361+
internal void SetCornerButtonState()
359362
{
363+
var stateName = VisualStates.StateNoButton;
364+
360365
if (TableView is ListView { SelectionMode: ListViewSelectionMode.Multiple })
361366
{
362-
VisualStates.GoToState(this, false, VisualStates.StateSelectAllCheckBox);
367+
stateName = TableView.IsEditing ? VisualStates.StateSelectAllCheckBoxDisabled : VisualStates.StateSelectAllCheckBox;
363368
}
364369
else if (TableView is { CornerButtonMode: TableViewCornerButtonMode.Options })
365370
{
366-
VisualStates.GoToState(this, false, VisualStates.StateOptionsButton);
371+
stateName = TableView.IsEditing ? VisualStates.StateOptionsButtonDisabled : VisualStates.StateOptionsButton;
367372
}
368373
else if (TableView is { CornerButtonMode: TableViewCornerButtonMode.SelectAll })
369374
{
370-
VisualStates.GoToState(this, false, VisualStates.StateSelectAllButton);
371-
}
372-
else
373-
{
374-
VisualStates.GoToState(this, false, VisualStates.StateNoButton);
375+
stateName = TableView.IsEditing ? VisualStates.StateSelectAllButtonDisabled : VisualStates.StateSelectAllButton;
375376
}
377+
378+
VisualStates.GoToState(this, false, stateName);
379+
}
380+
381+
/// <summary>
382+
/// Handles the SelectAllButton clicked event.
383+
/// </summary>
384+
private void OnSelectAllButtonClicked(object sender, TappedRoutedEventArgs e)
385+
{
386+
TableView?.SelectAll();
376387
}
377388

378389
/// <summary>
379390
/// Handles the Checked event for the select all checkbox.
380391
/// </summary>
381392
private void OnSelectAllCheckBoxChecked(object sender, RoutedEventArgs e)
382393
{
383-
TableView?.SelectAllSafe();
394+
TableView?.SelectAll();
384395
}
385396

386397
/// <summary>
@@ -479,12 +490,6 @@ private void OnTableViewChanged(DependencyPropertyChangedEventArgs e)
479490
newTableView.Columns.CollectionChanged += OnTableViewColumnsCollectionChanged;
480491
newTableView.Columns.ColumnPropertyChanged += OnColumnPropertyChanged;
481492

482-
_callbackTokens[ListViewBase.SelectionModeProperty] =
483-
newTableView.RegisterPropertyChangedCallback(ListViewBase.SelectionModeProperty, delegate { SetSelectAllButtonState(); });
484-
485-
_callbackTokens[TableView.CornerButtonModeProperty] =
486-
newTableView.RegisterPropertyChangedCallback(TableView.CornerButtonModeProperty, delegate { SetSelectAllButtonState(); });
487-
488493
_callbackTokens[TableView.ItemsSourceProperty] =
489494
newTableView.RegisterPropertyChangedCallback(TableView.ItemsSourceProperty, delegate { OnTableViewSelectionChanged(); });
490495
}

src/TableViewRow.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ private void OnContextRequested(UIElement sender, ContextRequestedEventArgs args
6060
TableView?.ShowRowContext(this, position);
6161
}
6262
}
63-
#endif
6463

6564
/// <summary>
6665
/// Determines if the context request is from a cell.
@@ -76,6 +75,7 @@ private bool IsContextRequestedFromCell(Windows.Foundation.Point position)
7675
.OfType<TableViewCell>()
7776
.Any();
7877
}
78+
#endif
7979

8080
/// <summary>
8181
/// Handles the IsSelected property changed.
@@ -189,7 +189,10 @@ protected override void OnContentChanged(object oldContent, object newContent)
189189

190190
protected override void OnPointerPressed(PointerRoutedEventArgs e)
191191
{
192-
base.OnPointerPressed(e);
192+
if (TableView is { IsEditing: false })
193+
{
194+
base.OnPointerPressed(e);
195+
}
193196

194197
if (!KeyboardHelper.IsShiftKeyDown() && TableView is not null)
195198
{
@@ -522,6 +525,16 @@ internal void EnsureAlternateColors()
522525
Index % 2 == 1 && TableView.AlternateRowForeground is not null ? TableView.AlternateRowForeground : _cellPresenterForeground;
523526
}
524527

528+
internal void UpdateSelectCheckMarkOpacity()
529+
{
530+
var fontIcon = this.FindDescendant<FontIcon>(x => x.Parent is Border);
531+
532+
if (fontIcon?.Parent is Border border)
533+
{
534+
border.Opacity = TableView?.IsEditing is true ? 0.3 : 1;
535+
}
536+
}
537+
525538
/// <summary>
526539
/// Gets the list of cells in the row.
527540
/// </summary>
@@ -551,7 +564,7 @@ internal set
551564

552565
public TableViewCellsPresenter? CellPresenter =>
553566
#if WINDOWS
554-
ContentTemplateRoot as TableViewCellsPresenter;
567+
ContentTemplateRoot as TableViewCellsPresenter;
555568
#else
556569
this.FindDescendant<TableViewCellsPresenter>();
557570
#endif

0 commit comments

Comments
 (0)