【问题标题】:Can not add rows to WPF DataGrid in PowerShell无法在 PowerShell 中向 WPF DataGrid 添加行
【发布时间】:2010-10-15 19:45:53
【问题描述】:

我正在使用来自 PowerShell 的“WPF Toolkit”中的 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"
  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() 

运行.bat

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)

有什么想法吗?

【问题讨论】:

    标签: wpf powershell datagrid wpftoolkit


    【解决方案1】:

    最终解决方案:

    # 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
    

    【讨论】:

      【解决方案2】:
      [System.Collections.ArrayList] $people = New-Object "System.Collections.ArrayList"
      

      如果你必须传递参数,你应该将它们设为强类型, 这样 PowerShell 就不会将其包装为 PsObject。

      Link no longer works

      【讨论】:

      • 链接已损坏;这就是为什么我们要求人们发布答案,而不是答案的链接。
      猜你喜欢
      • 2016-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-26
      • 1970-01-01
      • 2015-04-06
      • 1970-01-01
      • 2018-04-11
      相关资源
      最近更新 更多