PowerShell WinForms ListView 实现6个目录内容分6列展示的问题求助
核心问题根因
- WinForms ListView Details视图规则认知错误:视图内每一行是独立的
ListViewItem对象,第一列内容是Item本身绑定的文本,第二列及之后的内容是该Item挂载的子项(SubItem)。原代码每遍历一个目录条目就新建独立行,完全没有行级别的列映射逻辑,自然所有内容都会堆在第一列。 - 循环逻辑完全错位:遍历6个目录时,每一次循环都调用
Items.Add()新增空Item或独立行,没有把同一行下的各列内容绑定到同一个Item上。比如遍历Scripts目录内容时,直接把条目值作为字符串传给Items.Add(),占用第一列位置;后续目录的条目又各自开新行加子项,完全没有列对齐逻辑。 - 无效属性引用:代码中多次出现
$_.Scripts、$_.Null这类不存在的对象属性,实际生成的Item初始文本都是null空值,没有实际内容。 - 列宽自动调整时机错误:在ListView还没有加载任何条目时就调用
AutoResizeColumns,自动适配的宽度无参考对象,失去实际效果。
修正实现逻辑
要实现6列分别展示对应目录的条目,需要按行索引对齐各列内容:
- 按列的定义顺序,把6个目录的条目名分别存为独立的数组,增加目录存在性判断避免路径缺失时报错
- 取6个数组的最大长度,作为需要生成的ListView总行数
- 逐行生成
ListViewItem:第一列填入Scripts目录对应行索引的条目内容,剩余5个列依次填入其他目录对应行索引的条目,若某目录在该行索引下没有内容,则对应列填空字符串 - 所有行加载完成后,再触发列宽自动适配内容
可直接运行的修正脚本
Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Drawing $Form = New-Object Windows.Forms.Form $Form.Text = "多目录内容集中查看" $Form.Width = 1550 $Form.Height = 800 $Listview = New-Object System.Windows.Forms.ListView $Listview.Location = New-Object System.Drawing.Point(15,10) $Listview.View = [System.Windows.Forms.View]::Details $Listview.FullRowSelect = $true $Listview.GridLines = $true $Listview.Height = 650 $Listview.Width = 1500 $Listview.AllowColumnReorder = $true $Listview.Sorting = [System.Windows.Forms.SortOrder]::None # 按列顺序添加和目录一一对应的列头 [void]$Listview.Columns.Add('Scripts',150) [void]$Listview.Columns.Add('Applications',150) [void]$Listview.Columns.Add('Systems',200) [void]$Listview.Columns.Add('Databases',150) [void]$Listview.Columns.Add('Datas',150) [void]$Listview.Columns.Add('Backups',200) $oButton = New-Object Windows.Forms.Button $oButton.Text = "刷新列表" $oButton.Top = 700 $oButton.Left = 350 $oButton.Width = 150 $oButton.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom -bor [System.Windows.Forms.AnchorStyles]::Right # 采集6个目录的条目,目录不存在时返回空数组避免脚本崩溃 $dirContent = @( @(if (Test-Path "C:\Scripts") { (Get-ChildItem "C:\Scripts\").Name }), @(if (Test-Path "C:\Applications") { (Get-ChildItem "C:\Applications\").Name }), @(if (Test-Path "C:\Systems") { (Get-ChildItem "C:\Systems\").Name }), @(if (Test-Path "C:\Databases") { (Get-ChildItem "C:\Databases\").Name }), @(if (Test-Path "C:\Datas") { (Get-ChildItem "C:\Datas\").Name }), @(if (Test-Path "C:\Backups") { (Get-ChildItem "C:\Backups\").Name }) ) # 计算所有目录中的最大条目数,确定ListView总行数 $maxRowCount = ($dirContent | ForEach-Object { $_.Count } | Measure-Object -Maximum).Maximum # 逐行生成列表项,按行索引对齐各列内容 for ($i = 0; $i -lt $maxRowCount; $i++) { # 第一列内容:Scripts目录对应行的条目 $rowItem = New-Object System.Windows.Forms.ListViewItem( if ($i -lt $dirContent[0].Count) { $dirContent[0][$i] } else { "" } ) # 依次填充剩余5个列的内容 for ($col = 1; $col -lt 6; $col++) { $cellValue = if ($i -lt $dirContent[$col].Count) { $dirContent[$col][$i] } else { "" } $rowItem.SubItems.Add($cellValue) } [void]$Listview.Items.Add($rowItem) } # 所有行加载完成后再根据内容自动调整列宽 $Listview.AutoResizeColumns([System.Windows.Forms.ColumnHeaderAutoResizeStyle]::ColumnContent) $Form.Add_Shown({$Form.Activate()}) $Form.controls.add($oButton) $Form.controls.add($Listview) $Form.ShowDialog()
内容的提问来源于stack exchange,提问作者dst1ny
相关产品推荐
相关产品推荐

