这是一个小程序,它会在WPF Get-Childitem
中显示GridView
的结果。 '名称','长度',' LastWriteTime'和'模式'的列添加。
[void][System.Reflection.Assembly]::LoadWithPartialName('PresentationFramework')
$grid_view = New-Object -TypeName System.Windows.Controls.GridView
$property_names = 'Name', 'Length', 'LastWriteTime', 'Mode'
foreach ($elt in $property_names)
{
$grid_view.Columns.Add((New-Object -TypeName System.Windows.Controls.GridViewColumn `
-Property @{
Header = $elt
DisplayMemberBinding = New-Object System.Windows.Data.Binding -ArgumentList @(, $elt)
}))
}
$list_view = New-Object -TypeName System.Windows.Controls.ListView `
-Property @{
Name = 'abc'
Margin = New-Object System.Windows.Thickness -ArgumentList @(, 10)
View = $grid_view
}
Get-ChildItem | ForEach-Object { $list_view.Items.Add($_) | Out-Null }
$grid = New-Object System.Windows.Controls.Grid
$grid.Children.Add($list_view) | Out-Null
$window = New-Object System.Windows.Window -Property @{ Content = $grid }
$window.ShowDialog() | Out-Null
这是生成的窗口:
请注意'模式'列是空的。
'模式'有什么特别之处?属性?那么,让我们仔细看看它:
PS C:\> Get-ChildItem C:\Windows\notepad.exe | Get-Member | Where-Object { $_.MemberType -match 'property' }
TypeName: System.IO.FileInfo
Name MemberType Definition
---- ---------- ----------
Mode CodeProperty System.String Mode{get=Mode;}
PSChildName NoteProperty System.String PSChildName=notepad.exe
PSDrive NoteProperty System.Management.Automation.PSDriveInfo PSDrive=C
PSIsContainer NoteProperty System.Boolean PSIsContainer=False
PSParentPath NoteProperty System.String PSParentPath=Microsoft.PowerShell.Core\FileSy...
PSPath NoteProperty System.String PSPath=Microsoft.PowerShell.Core\FileSystem::...
PSProvider NoteProperty System.Management.Automation.ProviderInfo PSProvider=Micros...
Attributes Property System.IO.FileAttributes Attributes {get;set;}
CreationTime Property datetime CreationTime {get;set;}
CreationTimeUtc Property datetime CreationTimeUtc {get;set;}
Directory Property System.IO.DirectoryInfo Directory {get;}
DirectoryName Property string DirectoryName {get;}
Exists Property bool Exists {get;}
Extension Property string Extension {get;}
FullName Property string FullName {get;}
IsReadOnly Property bool IsReadOnly {get;set;}
LastAccessTime Property datetime LastAccessTime {get;set;}
LastAccessTimeUtc Property datetime LastAccessTimeUtc {get;set;}
LastWriteTime Property datetime LastWriteTime {get;set;}
LastWriteTimeUtc Property datetime LastWriteTimeUtc {get;set;}
Length Property long Length {get;}
Name Property string Name {get;}
BaseName ScriptProperty System.Object BaseName {get=if ($this.Extension.Length -gt ...
VersionInfo ScriptProperty System.Object VersionInfo {get=[System.Diagnostics.FileVers...
我们可以看到Mode
是CodeProperty
,而正确显示的属性属于MemberType
Property
。
如果我们更改此行:
Get-ChildItem | ForEach-Object { $list_view.Items.Add($_) | Out-Null }
到这一行:
Get-ChildItem | select $property_names | ForEach-Object { $list_view.Items.Add($_) | Out-Null }
出现Mode
列值! :
这里发生了什么?好吧,通过运行Get-ChildItem
到Select-Object
的输出并显式选择属性,MemberTypes将转换为NoteProperty
类型:
PS C:\> Get-ChildItem C:\Windows\notepad.exe | select Name, Mode | Get-Member
TypeName: Selected.System.IO.FileInfo
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
Mode NoteProperty System.String Mode=-a---
Name NoteProperty System.String Name=notepad.exe
显然,CodeProperty
属性在这种情况下似乎没有正确绑定。
当然,我希望将Get-Chiditem
的结果直接存储在ListView
而不是Selected
变种中。
所以我的问题是,有没有办法在列中正确显示CodeProperty
?
答案 0 :(得分:0)
看起来DataGrid
处理Mode
属性就好了。
这是一个名为Out-DataGrid
的函数,它使用DataGrid
代替GridView
和ListView
:
[空隙] [System.Reflection.Assembly] :: LoadWithPartialName(' PresentationFramework&#39)
函数Out-DataGrid($ properties) {
$data_grid = New-Object -TypeName System.Windows.Controls.DataGrid
$data_grid.IsReadOnly = $true
$data_grid.AutoGenerateColumns = $false
foreach ($elt in $properties)
{
$data_grid.Columns.Add((New-Object -TypeName System.Windows.Controls.DataGridTextColumn `
-Property @{
Header = $elt
Binding = (New-Object System.Windows.Data.Binding -ArgumentList @(, $elt))
}))
}
$data_grid.ItemsSource = @($input)
$grid = New-Object System.Windows.Controls.Grid
$grid.Children.Add($data_grid) | Out-Null
$window = New-Object System.Windows.Window -Property @{ Content = $grid }
$window.ShowDialog() | Out-Null
$data_grid.SelectedItem
}
使用示例:
ls | Out-DataGrid -properties 'Name', 'Mode', 'Length'