diff --git a/src/WinUI.TableView/Extensions/TableViewCellSlotExtensions.cs b/src/WinUI.TableView/Extensions/TableViewCellSlotExtensions.cs new file mode 100644 index 0000000..1e9f574 --- /dev/null +++ b/src/WinUI.TableView/Extensions/TableViewCellSlotExtensions.cs @@ -0,0 +1,19 @@ +namespace WinUI.TableView.Extensions; + +internal static class TableViewCellSlotExtensions +{ + public static bool IsValidRow(this TableViewCellSlot slot, TableView tableView) + { + return slot.Row >= 0 && slot.Row < tableView?.Items.Count; + } + + public static bool IsValidColumn(this TableViewCellSlot slot, TableView tableView) + { + return slot.Column >= 0 && slot.Column < tableView?.Columns.VisibleColumns.Count; + } + + public static bool IsValid(this TableViewCellSlot slot, TableView tableView) + { + return slot.IsValidRow(tableView) && slot.IsValidColumn(tableView); + } +} diff --git a/src/WinUI.TableView/TableView.Properties.cs b/src/WinUI.TableView/TableView.Properties.cs index 69718be..ed48677 100644 --- a/src/WinUI.TableView/TableView.Properties.cs +++ b/src/WinUI.TableView/TableView.Properties.cs @@ -28,6 +28,7 @@ public partial class TableView public IAdvancedCollectionView CollectionView { get; private set; } = new AdvancedCollectionView(); internal IDictionary> ActiveFilters { get; } = new Dictionary>(); + internal TableViewSelectionUnit LastSelectionUnit { get; set; } internal TableViewCellSlot? CurrentCellSlot { get; set; } internal TableViewCellSlot? SelectionStartCellSlot { get; set; } internal int? SelectionStartRowIndex { get; set; } diff --git a/src/WinUI.TableView/TableView.cs b/src/WinUI.TableView/TableView.cs index 38b3f65..df83342 100644 --- a/src/WinUI.TableView/TableView.cs +++ b/src/WinUI.TableView/TableView.cs @@ -158,7 +158,7 @@ private void HandleNavigations(KeyRoutedEventArgs e, bool shiftKey, bool ctrlKey else if ((e.Key is VirtualKey.Left or VirtualKey.Right or VirtualKey.Up or VirtualKey.Down) && !IsEditing) { - var row = (SelectionUnit is TableViewSelectionUnit.Row ? SelectionStartRowIndex : SelectionStartCellSlot?.Row) ?? -1; + var row = (LastSelectionUnit is TableViewSelectionUnit.Row ? SelectionStartRowIndex : SelectionStartCellSlot?.Row) ?? -1; var column = CurrentCellSlot?.Column ?? -1; if (row == -1 && column == -1) @@ -168,6 +168,11 @@ private void HandleNavigations(KeyRoutedEventArgs e, bool shiftKey, bool ctrlKey else if (e.Key is VirtualKey.Left or VirtualKey.Right) { column = e.Key is VirtualKey.Left ? ctrlKey ? 0 : column - 1 : ctrlKey ? Columns.VisibleColumns.Count - 1 : column + 1; + if (column >= Columns.VisibleColumns.Count) + { + column = 0; + row++; + } } else { @@ -612,11 +617,6 @@ internal void ClearFilters() } } - private bool IsValidSlot(TableViewCellSlot slot) - { - return slot.Row >= 0 && slot.Column >= 0 && slot.Row < Items.Count && slot.Column < Columns.VisibleColumns.Count; - } - internal new void SelectAll() { if (IsEditing) @@ -703,7 +703,7 @@ private void DeselectAllCells() internal void MakeSelection(TableViewCellSlot slot, bool shiftKey, bool ctrlKey = false) { - if (slot.Row < 0 || slot.Row >= Items.Count) + if (!slot.IsValidRow(this)) { return; } @@ -725,13 +725,17 @@ internal void MakeSelection(TableViewCellSlot slot, bool shiftKey, bool ctrlKey } } - if (SelectionUnit is TableViewSelectionUnit.Row) + if (SelectionUnit is TableViewSelectionUnit.Row + || (LastSelectionUnit is TableViewSelectionUnit.Row && slot.IsValidRow(this) && !slot.IsValidColumn(this)) + || (SelectionUnit is TableViewSelectionUnit.CellOrRow && slot.IsValidRow(this) && !slot.IsValidColumn(this))) { SelectRows(slot, shiftKey); + LastSelectionUnit = TableViewSelectionUnit.Row; } else { SelectCells(slot, shiftKey); + LastSelectionUnit = TableViewSelectionUnit.Cell; } } else if (!IsReadOnly) @@ -771,7 +775,7 @@ private void SelectRows(TableViewCellSlot slot, bool shiftKey) } } - if (!IsReadOnly && IsValidSlot(slot)) + if (!IsReadOnly && slot.IsValid(this)) { DispatcherQueue.TryEnqueue(() => SetCurrentCell(slot)); } @@ -787,7 +791,7 @@ private void SelectRows(TableViewCellSlot slot, bool shiftKey) private void SelectCells(TableViewCellSlot slot, bool shiftKey) { - if (!IsValidSlot(slot)) + if (!slot.IsValid(this)) { return; } @@ -896,7 +900,7 @@ private void OnCellSelectionChanged() internal async Task ScrollCellIntoView(TableViewCellSlot slot) { - if (_scrollViewer is null || !IsValidSlot(slot)) return default!; + if (_scrollViewer is null || !slot.IsValid(this)) return default!; var row = await ScrollRowIntoView(slot.Row); var (start, end) = GetColumnsInDisplay(); @@ -1007,7 +1011,7 @@ void ViewChanged(object? _, ScrollViewerViewChangedEventArgs e) internal TableViewCell GetCellFromSlot(TableViewCellSlot slot) { - return IsValidSlot(slot) && ContainerFromIndex(slot.Row) is TableViewRow row ? row.Cells[slot.Column] : default!; + return slot.IsValid(this) && ContainerFromIndex(slot.Row) is TableViewRow row ? row.Cells[slot.Column] : default!; } private (int start, int end) GetColumnsInDisplay() diff --git a/src/WinUI.TableView/TableViewRow.cs b/src/WinUI.TableView/TableViewRow.cs index 76dbbee..31ca609 100644 --- a/src/WinUI.TableView/TableViewRow.cs +++ b/src/WinUI.TableView/TableViewRow.cs @@ -54,7 +54,17 @@ protected override void OnPointerReleased(PointerRoutedEventArgs e) if (!KeyBoardHelper.IsShiftKeyDown() && TableView is not null) { TableView.SelectionStartCellSlot = null; - TableView.SelectionStartRowIndex = null; + TableView.SelectionStartRowIndex = Index; + } + } + + protected override void OnTapped(TappedRoutedEventArgs e) + { + base.OnTapped(e); + + if (TableView?.SelectionUnit is TableViewSelectionUnit.Row or TableViewSelectionUnit.CellOrRow) + { + TableView.LastSelectionUnit = TableViewSelectionUnit.Row; } }