Refactor and improve ListView item selection

Extracts the logic for toggling an item's selection state with the spacebar into a new, reusable function. This change centralizes previously duplicated code, improving maintainability.

The event handling is moved from `KeyDown` to `PreviewKeyDown` to provide a more reliable user experience. Additionally, focus is now correctly restored to the toggled item, preventing the selection from moving unexpectedly.
This commit is contained in:
rbalsleyMSFT
2025-06-20 11:50:47 -07:00
parent 86ec6de894
commit 1675c48fc3
2 changed files with 73 additions and 11 deletions
@@ -441,6 +441,47 @@ function Update-SelectAllHeaderCheckBoxState {
}
}
# Function to toggle the IsSelected state of the currently selected ListView item
function Invoke-ListViewItemToggle {
param(
[Parameter(Mandatory)]
[System.Windows.Controls.ListView]$ListView,
[Parameter(Mandatory)]
[psobject]$State,
[Parameter(Mandatory)]
[string]$HeaderCheckBoxKeyName
)
$selectedItem = $ListView.SelectedItem
if ($null -eq $selectedItem) { return }
# Store the current index to restore focus later
$currentIndex = $ListView.SelectedIndex
# Toggle the IsSelected property
$selectedItem.IsSelected = -not $selectedItem.IsSelected
$ListView.Items.Refresh()
# Update the 'Select All' header checkbox state
$headerChk = $State.Controls[$HeaderCheckBoxKeyName]
if ($null -ne $headerChk) {
Update-SelectAllHeaderCheckBoxState -ListView $ListView -HeaderCheckBox $headerChk
}
# Restore selection and focus to the item that was just toggled
if ($currentIndex -ge 0 -and $ListView.Items.Count -gt $currentIndex) {
$ListView.SelectedIndex = $currentIndex
# Ensure the UI is updated before trying to find the container
$ListView.UpdateLayout()
$listViewItem = $ListView.ItemContainerGenerator.ContainerFromIndex($currentIndex)
if ($null -ne $listViewItem) {
$listViewItem.Focus()
}
}
}
# Function to sort ListView items
function Invoke-ListViewSort {
param(