无法将行添加到PowerShell中的WPF DataGrid
我正在使用PowerShell“WPF工具包”中的DataGrid。问题是我无法使用GUI添加新行 dialog.xaml无法将行添加到PowerShell中的WPF DataGrid,wpf,powershell,datagrid,wpftoolkit,Wpf,Powershell,Datagrid,Wpftoolkit,我正在使用PowerShell“WPF工具包”中的DataGrid。问题是我无法使用GUI添加新行 dialog.xaml <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sys="clr-namespace:System;assembly=mscorlib"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:dg="clr-namespace:Microsoft.Windows.Controls;assembly=WpfToolkit"
>
<Window.Resources>
<x:Array x:Key="people" Type="sys:Object" />
</Window.Resources>
<StackPanel>
<dg:DataGrid x:Name="_grid" ItemsSource="{DynamicResource people}" CanUserAddRows="True" AutoGenerateColumns="False">
<dg:DataGrid.Columns>
<dg:DataGridTextColumn Header="First" Binding="{Binding First}"></dg:DataGridTextColumn>
<dg:DataGridTextColumn Header="Last" Binding="{Binding Last}"></dg:DataGridTextColumn>
</dg:DataGrid.Columns>
</dg:DataGrid>
<Button>test</Button>
</StackPanel>
</Window>
跑吧,蝙蝠
powershell -sta -file dialog.ps1
问题似乎出在$people集合中。我在C#中尝试了相同的代码,并且成功了,但是集合是这样定义的:
List<Person> people = new List<Person>();
people.Add(new Person { First = "John", Last = "Smith" });
this.Resources["people"] = people;
有什么想法吗
[System.Collections.ArrayList] $people = New-Object "System.Collections.ArrayList"
如果必须传递参数,则应将其设为强类型,
因此PowerShell不会将其包装为PsObject
最终解决方案:
# Declare Person class
add-type @"
public class Person
{
public Person() {}
public string First { get; set; }
public string Last { get; set; }
}
"@ -Language CsharpVersion3
# Make strongly-typed collection
[System.Collections.ArrayList] $people = New-Object "System.Collections.ArrayList"
#
$john = new-object Person
$john.First = "John"
$john.Last = "Smith"
$people.add($john)
$form.Resources["people"] = $people
链路断开;这就是为什么我们要求人们发布答案,而不是答案的链接。
[System.Collections.ArrayList] $people = New-Object "System.Collections.ArrayList"
# Declare Person class
add-type @"
public class Person
{
public Person() {}
public string First { get; set; }
public string Last { get; set; }
}
"@ -Language CsharpVersion3
# Make strongly-typed collection
[System.Collections.ArrayList] $people = New-Object "System.Collections.ArrayList"
#
$john = new-object Person
$john.First = "John"
$john.Last = "Smith"
$people.add($john)
$form.Resources["people"] = $people