2009-03-26 6 views
0

PowerShell의 "WPF Toolkit"에서 DataGrid를 사용하고 있습니다. 문제는 GUI를 사용하여 새 행을 추가 할 수 없다는 것입니다.PowerShell의 WPF DataGrid에 행을 추가 할 수 없습니다.

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

powershell -sta -file 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를. 나는 C#에서 동일한 코드를 tryed하고 그것을 작동했지만 컬렉션이 방법 정의 :

List<Person> people = new List<Person>(); 
people.Add(new Person { First = "John", Last = "Smith" }); 
this.Resources["people"] = people; 

는 또한 CLR 수집을 tryed을 - 전혀 작동하지 않았다

$people = New-Object "System.Collections.Generic.List``1[System.Object]" 
$people.add($john) 

어떤 아이디어? 당신이 인수를 전달해야하는 경우

답변

0

[System.Collections.ArrayList] $ 사람들이 = 새 객체 "System.Collections.ArrayList"

, 당신은 그들에게 강력한 형식해야한다, 그래서 PowerShell은 '아무튼 있음 PsObject로 감 쌉니다.

http://poshcode.org/68

0

최종 용액 :

# 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