无法在 PowerShell 中向 WPF DataGrid 添加行
我正在使用 PowerShell 中“WPF Toolkit”中的 DataGrid。 问题是我无法使用 GUI 添加新行。
dialog.xamldialog.ps1run.bat
<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>
中
# Includes
Add-Type -AssemblyName PresentationFramework
[System.Reflection.Assembly]::LoadFrom("C:\Program Files\WPF Toolkit\v3.5.40320.1\WPFToolkit.dll")
# Helper methods
function LoadXaml
{
param($fileName)
[xml]$xaml = [IO.File]::ReadAllText($fileName)
$reader = (New-Object System.Xml.XmlNodeReader $xaml)
[Windows.Markup.XamlReader]::Load( $reader )
}
# Load XAML
$form = LoadXaml('.\dialog.xaml')
#
$john = new-object PsObject
$john | Add-Member -MemberType NoteProperty -Name "First" -Value ("John")
$john | Add-Member -MemberType NoteProperty -Name "Last" -Value ("Smith")
$people = @( $john )
$form.Resources["people"] = $people
#
$form.ShowDialog()
。
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;
还尝试了 Clr 集合 - 根本不起作用:
$people = New-Object "System.Collections.Generic.List``1[System.Object]"
$people.add($john)
有什么想法吗?
I am using DataGrid from "WPF Toolkit" from PowerShell. The problem is that I can't add new rows using 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"
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>
dialog.ps1
# Includes
Add-Type -AssemblyName PresentationFramework
[System.Reflection.Assembly]::LoadFrom("C:\Program Files\WPF Toolkit\v3.5.40320.1\WPFToolkit.dll")
# Helper methods
function LoadXaml
{
param($fileName)
[xml]$xaml = [IO.File]::ReadAllText($fileName)
$reader = (New-Object System.Xml.XmlNodeReader $xaml)
[Windows.Markup.XamlReader]::Load( $reader )
}
# Load XAML
$form = LoadXaml('.\dialog.xaml')
#
$john = new-object PsObject
$john | Add-Member -MemberType NoteProperty -Name "First" -Value ("John")
$john | Add-Member -MemberType NoteProperty -Name "Last" -Value ("Smith")
$people = @( $john )
$form.Resources["people"] = $people
#
$form.ShowDialog()
run.bat
powershell -sta -file dialog.ps1
The problem seems to be in $people collection. I have tryed same code in C# and it worked, but the collection was defined this way:
List<Person> people = new List<Person>();
people.Add(new Person { First = "John", Last = "Smith" });
this.Resources["people"] = people;
Also tryed the Clr collection - did not work at all:
$people = New-Object "System.Collections.Generic.List``1[System.Object]"
$people.add($john)
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最终的解决方案:
The final solution:
链接不再有效
Link no longer works