我尝试在PowerShell中创建一个UI,其DataGrid
更新为源更新,因此我使用ObservableCollection<PSObject>
。但是,当我将其设置为ItemsSource
时,DataGrid
不会生成任何列。如果我使用原始数组,它确实如此。这是一个例子:
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
答案 0 :(得分:2)
解决方案是将ObservableCollection<PSObject>
替换为ObservableCollection<Object>
。