這裏有一個小程序,它會顯示在WPF GridView
的Get-Childitem
結果。添加'姓名','長度','LastWriteTime'和'模式'的列。GridView控件 - 如何綁定CodeProperty
[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
列值出現! :
這是怎麼回事?好了,通過Select-Object
運行Get-ChildItem
輸出,並明確選擇屬性,在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
列正確顯示?