我想在PowerShell中創建一個UI,其中DataGrid
作爲源更新進行更新,因此我使用的是ObservableCollection<PSObject>
。但是當我將其設置爲ItemsSource
時,DataGrid
不會生成任何列。如果我使用原始數組,它的確如此。這裏有一個例子:我應該能夠使用ObservableCollection [PSObject]作爲DataGrid的ItemsSource嗎?
Add-Type -AssemblyName PresentationCore
Add-Type -AssemblyName PresentationFramework
Add-Type -AssemblyName WindowsBase
[xml]$xaml = @"
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Height="500"
Width="600">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<DataGrid Grid.Row="0"
x:Name="dataGrid"
IsReadOnly="True"
Margin="6" />
<DataGrid Grid.Row="1"
x:Name="observableDataGrid"
IsReadOnly="True"
Margin="6" />
</Grid>
</Window>
"@
$reader = (New-Object System.Xml.XmlNodeReader $xaml)
$Window = [Windows.Markup.XamlReader]::Load($reader)
$dataGrid = $window.FindName("dataGrid")
$observableDataGrid = $window.FindName("observableDataGrid")
$results = Get-Process | Select-Object -Property ProcessName,Id,Handles,NPM,PM,VM,WS
$results2 = New-Object -TypeName System.Collections.ObjectModel.ObservableCollection[PSObject] -ArgumentList @(,$results)
$dataGrid.ItemsSource = $results
$observableDataGrid.ItemsSource = $results2
$Window.ShowDialog() | Out-Null
嘗試將datagrid的自動生成列設置爲true,看看是否有幫助。還要確保該集合具有元素並且它們具有屬性,字段可能不起作用。 – pushpraj 2014-09-03 06:07:30