我需要在ListView中添加一组项目,但$listView1.groups.add("groupName")
不起作用!
$button1.add_click({
# add group
$listView1.groups.add("groupName") # Not Work
# add items
1..3 | %{
$listView1.items.add("Item : $_") # work fine
}
})
完整代码
#requires -version 2
Add-Type -AssemblyName 'System.Drawing'
Add-Type -AssemblyName 'System.Windows.Forms'
#region $form1
$form1 = New-Object -TypeName 'System.Windows.Forms.Form'
$form1.SuspendLayout()
#region $listView1
$listView1 = New-Object -TypeName 'System.Windows.Forms.ListView'
$listView1.Dock = [System.Windows.Forms.DockStyle]::Fill
$listView1.View = [System.Windows.Forms.View]::Tile
$listView1.Size = New-Object -TypeName 'System.Drawing.Size' -ArgumentList @(250, 200)
$listView1.BeginUpdate()
#region $listViewGroup1
$listViewGroup1 = New-Object -TypeName 'System.Windows.Forms.ListViewGroup'
$listViewGroup1.Header = 'Group 1'
#region $listViewItem1
$listViewItem1 = New-Object -TypeName 'System.Windows.Forms.ListViewItem'
$listViewItem1.Text = 'Item 1'
#endregion $listViewItem1
$listViewItem1.Group = $listViewGroup1
#region $listViewItem2
$listViewItem2 = New-Object -TypeName 'System.Windows.Forms.ListViewItem'
$listViewItem2.Text = 'Item 2'
#endregion $listViewItem2
$listViewItem2.Group = $listViewGroup1
#endregion $listViewGroup1
[System.Void]$listView1.Groups.Add($listViewGroup1)
[System.Void]$listView1.Items.Add($listViewItem1)
[System.Void]$listView1.Items.Add($listViewItem2)
#region $listViewGroup2
$listViewGroup2 = New-Object -TypeName 'System.Windows.Forms.ListViewGroup'
$listViewGroup2.Header = 'Group 2'
#region $listViewItem3
$listViewItem3 = New-Object -TypeName 'System.Windows.Forms.ListViewItem'
$listViewItem3.Text = 'Item 3'
#endregion $listViewItem3
$listViewItem3.Group = $listViewGroup2
#endregion $listViewGroup2
[System.Void]$listView1.Groups.Add($listViewGroup2)
[System.Void]$listView1.Items.Add($listViewItem3)
$listView1.EndUpdate()
#endregion $listView1
[System.Void]$form1.Controls.Add($listView1)
#region $button1
$button1 = New-Object -TypeName 'System.Windows.Forms.Button'
$button1.Dock = [System.Windows.Forms.DockStyle]::Bottom
$button1.Text = 'Add Group'
$button1.add_click({
# add group
$listView1.groups.add("groupName") # Not Work
# add items
1..3 | %{
$listView1.items.add("Item : $_") # work fine
}
})
#endregion $button1
[System.Void]$form1.Controls.Add($button1)
$form1.ResumeLayout($false)
$form1.PerformLayout()
#endregion $form1
#region GUI Startup
$form1.ShowDialog()
#endregion GUI Startup
答案 0 :(得分:1)
建立一个像这样的对象是必要的:
#region $listViewGroup1
$listViewGroup1 = New-Object -TypeName 'System.Windows.Forms.ListViewGroup'
$listViewGroup1.Header = 'Group 3211'
[System.Void]$listView1.Groups.Add($listViewGroup1)
$listViewItem1 = New-Object -TypeName 'System.Windows.Forms.ListViewItem'
$listViewItem1.Text = 'Item 3211'
$listViewItem1.Group = $listViewGroup1
[System.Void]$listView1.Items.Add($listViewItem1)
$listViewItem2 = New-Object -TypeName 'System.Windows.Forms.ListViewItem'
$listViewItem2.Text = 'Item 3212'
$listViewItem2.Group = $listViewGroup1
[System.Void]$listView1.Items.Add($listViewItem2)