@@ -49,7 +49,7 @@ function Invoke-WPFUIElements {
4949 # Add ColumnDefinitions to the target Grid
5050 for ($i = 0 ; $i -lt $columncount ; $i ++ ) {
5151 $colDef = New-Object Windows.Controls.ColumnDefinition
52- $colDef.Width = New-Object Windows.GridLength(1 , [Windows.GridUnitType ]::Star)
52+ $colDef.Width = New-Object System. Windows.GridLength([ double ] 1 , [System. Windows.GridUnitType ]::Star)
5353 $targetGrid.ColumnDefinitions.Add ($colDef ) | Out-Null
5454 }
5555
@@ -113,36 +113,55 @@ function Invoke-WPFUIElements {
113113 $dockPanelContainer = New-Object Windows.Controls.DockPanel
114114 $border.Child = $dockPanelContainer
115115
116- # Create an ItemsControl for application content
117- $itemsControl = New-Object Windows.Controls.ItemsControl
118- $itemsControl.HorizontalAlignment = ' Stretch'
119- $itemsControl.VerticalAlignment = ' Stretch'
120-
121- # Set the ItemsPanel to a VirtualizingStackPanel
122- $itemsPanelTemplate = New-Object Windows.Controls.ItemsPanelTemplate
123- $factory = New-Object Windows.FrameworkElementFactory ([Windows.Controls.VirtualizingStackPanel ])
124- $itemsPanelTemplate.VisualTree = $factory
125- $itemsControl.ItemsPanel = $itemsPanelTemplate
126-
127- # Set virtualization properties
128- $itemsControl.SetValue ([Windows.Controls.VirtualizingStackPanel ]::IsVirtualizingProperty, $true )
129- $itemsControl.SetValue ([Windows.Controls.VirtualizingStackPanel ]::VirtualizationModeProperty, [Windows.Controls.VirtualizationMode ]::Recycling)
116+ # Create a StackPanel for application content controls
117+ $stackPanelContainer = New-Object Windows.Controls.StackPanel
118+ $stackPanelContainer.HorizontalAlignment = ' Stretch'
119+ $stackPanelContainer.VerticalAlignment = ' Stretch'
120+
121+ # Check if the target grid (or any ancestor) is already inside a ScrollViewer
122+ $hasOuterScrollViewer = $false
123+ $currentElement = $targetGrid
124+ while ($null -ne $currentElement ) {
125+ if ($currentElement -is [System.Windows.Controls.ScrollViewer ] -or $currentElement.GetType ().Name -eq " ScrollViewer" ) {
126+ $hasOuterScrollViewer = $true
127+ break
128+ }
129+ $currentElement = $currentElement.Parent
130+ }
130131
131- # Add the ItemsControl directly to the DockPanel
132- [Windows.Controls.DockPanel ]::SetDock($itemsControl , [Windows.Controls.Dock ]::Bottom)
133- $dockPanelContainer.Children.Add ($itemsControl ) | Out-Null
132+ if ($hasOuterScrollViewer ) {
133+ # Add StackPanel directly to DockPanel without nesting a ScrollViewer
134+ [Windows.Controls.DockPanel ]::SetDock($stackPanelContainer , [Windows.Controls.Dock ]::Bottom)
135+ $dockPanelContainer.Children.Add ($stackPanelContainer ) | Out-Null
136+ }
137+ else {
138+ # Create a ScrollViewer for targets that do not already have an outer ScrollViewer
139+ $scrollViewer = New-Object Windows.Controls.ScrollViewer
140+ $scrollViewer.VerticalScrollBarVisibility = " Auto"
141+ $scrollViewer.HorizontalScrollBarVisibility = " Disabled"
142+ $scrollViewer.HorizontalAlignment = ' Stretch'
143+ $scrollViewer.VerticalAlignment = ' Stretch'
144+ $scrollViewer.Content = $stackPanelContainer
145+
146+ [Windows.Controls.DockPanel ]::SetDock($scrollViewer , [Windows.Controls.Dock ]::Bottom)
147+ $dockPanelContainer.Children.Add ($scrollViewer ) | Out-Null
148+ }
134149 $panelcount ++
135150
136- # Now proceed with adding category labels and entries to $itemsControl
151+ # Now proceed with adding category labels and entries to $stackPanelContainer
137152 foreach ($category in ($organizedData [$panelKey ].Keys | Sort-Object )) {
138153 $count ++
139154
140155 $label = New-Object Windows.Controls.Label
141- $label.Content = $category -replace " .*__" , " "
156+ $categoryCleanName = $category -replace " .*__" , " "
157+ $label.Content = $categoryCleanName
158+ $label.Focusable = $true
159+ $label.IsTabStop = $true
160+ [System.Windows.Automation.AutomationProperties ]::SetName($label , $categoryCleanName )
142161 $label.SetResourceReference ([Windows.Controls.Control ]::FontSizeProperty, " HeaderFontSize" )
143162 $label.SetResourceReference ([Windows.Controls.Control ]::FontFamilyProperty, " HeaderFontFamily" )
144163 $label.UseLayoutRounding = $true
145- $itemsControl .Items .Add ($label ) | Out-Null
164+ $stackPanelContainer .Children .Add ($label ) | Out-Null
146165 $sync [$category ] = $label
147166
148167 # Sort entries by type (checkboxes first, then buttons, then comboboxes, notes last) and then alphabetically by Content
@@ -177,7 +196,7 @@ function Invoke-WPFUIElements {
177196 $label.SetResourceReference ([Windows.Controls.Control ]::ForegroundProperty, " MainForegroundColor" )
178197 $label.UseLayoutRounding = $true
179198 $dockPanel.Children.Add ($label ) | Out-Null
180- $itemsControl .Items .Add ($dockPanel ) | Out-Null
199+ $stackPanelContainer .Children .Add ($dockPanel ) | Out-Null
181200
182201 $sync [$entryInfo.Name ] = $checkBox
183202 $sync [$entryInfo.Name ].IsChecked = (Get-WinUtilToggleStatus $entryInfo.Name )
@@ -215,7 +234,7 @@ function Invoke-WPFUIElements {
215234 contentOff = if ($entryInfo.Content.Count -ge 2 ) { $entryInfo.Content [1 ] } else { $contentOn }
216235 }
217236
218- $itemsControl .Items .Add ($toggleButton ) | Out-Null
237+ $stackPanelContainer .Children .Add ($toggleButton ) | Out-Null
219238
220239 $sync [$entryInfo.Name ] = $toggleButton
221240
@@ -295,7 +314,7 @@ function Invoke-WPFUIElements {
295314 }
296315
297316 $horizontalStackPanel.Children.Add ($comboBox ) | Out-Null
298- $itemsControl .Items .Add ($horizontalStackPanel ) | Out-Null
317+ $stackPanelContainer .Children .Add ($horizontalStackPanel ) | Out-Null
299318
300319 if ($entryInfo.Registry -and @ ($entryInfo.Registry )[0 ].Values) {
301320 try {
@@ -387,7 +406,7 @@ function Invoke-WPFUIElements {
387406 $button.Width = [math ]::Max($baseWidth , 350 )
388407 }
389408 [System.Windows.Automation.AutomationProperties ]::SetName($button , $entryInfo.Content )
390- $itemsControl .Items .Add ($button ) | Out-Null
409+ $stackPanelContainer .Children .Add ($button ) | Out-Null
391410
392411 $sync [$entryInfo.Name ] = $button
393412
@@ -414,7 +433,7 @@ function Invoke-WPFUIElements {
414433 $radioButtonGroups [$entryInfo.GroupName ] = $groupStackPanel
415434
416435 # Add the group container to the ItemsControl
417- $itemsControl .Items .Add ($groupStackPanel ) | Out-Null
436+ $stackPanelContainer .Children .Add ($groupStackPanel ) | Out-Null
418437 }
419438 else {
420439 # Retrieve the existing group container
@@ -459,7 +478,7 @@ function Invoke-WPFUIElements {
459478 $textBlock.Inlines.Add ($bulletBadge )
460479 $textBlock.Inlines.Add ($textRun )
461480
462- $itemsControl .Items .Add ($textBlock ) | Out-Null
481+ $stackPanelContainer .Children .Add ($textBlock ) | Out-Null
463482 }
464483
465484 default {
@@ -519,7 +538,7 @@ function Invoke-WPFUIElements {
519538 $sync [$textBlock.Name ] = $textBlock
520539 }
521540
522- $itemsControl .Items .Add ($horizontalStackPanel ) | Out-Null
541+ $stackPanelContainer .Children .Add ($horizontalStackPanel ) | Out-Null
523542 $sync [$entryInfo.Name ] = $checkBox
524543
525544 $sync [$entryInfo.Name ].Add_Checked({
0 commit comments