2016-07-02 51 views
0

這裏有一個小程序,它會顯示在WPF GridViewGet-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 

這裏出現的窗口:

enter image description here

注意, '模式' 欄爲空。

什麼特別之處「模式」屬性?好吧,讓我們來仔細看看吧:

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... 

我們可以看到,ModeCodeProperty,而這正確顯示是MemberTypeProperty屬性。

如果我們改變這一行:

Get-ChildItem | ForEach-Object { $list_view.Items.Add($_) | Out-Null } 

這一行:

Get-ChildItem | select $property_names | ForEach-Object { $list_view.Items.Add($_) | Out-Null } 

Mode列值出現! :

enter image description here

這是怎麼回事?好了,通過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列正確顯示?

回答

0

它看起來像DataGrid處理Mode屬性就好了。

這裏有一個名爲Out-DataGrid功能,它使用DataGrid而不是GridViewListView

[空] [的System.Reflection。組件] :: LoadWithPartialName( 'PresentationFramework')

功能OUT-數據網格($屬性) {

$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' 

enter image description here