gpt4 book ai didi

wpf - 我应该能够将ObservableCollection [PSObject]用作DataGrid的ItemsSource吗?

转载 作者:行者123 更新时间:2023-12-02 23:45:05 24 4
gpt4 key购买 nike

我正在尝试在PowerShell中创建一个具有DataGrid且随源更新而更新的UI,所以我正在使用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

最佳答案

解决方案是将ObservableCollection<PSObject>替换为ObservableCollection<Object>

关于wpf - 我应该能够将ObservableCollection [PSObject]用作DataGrid的ItemsSource吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25637033/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com