How do I bind data in a DataGrid?

May 15, 2011 at 4:18 PM

ipmo ShowUI

if(!(Get-Command DataGrid -ErrorAction SilentlyContinue)) {
    Add-UIFunction -Type System.Windows.Controls.DataGrid
}

if(!(Get-Command DataGridTextColumn -ErrorAction SilentlyContinue)) {
    Add-UIFunction -Type  System.Windows.Controls.DataGridTextColumn
}

$data = @{
    Subject = "This is a Subject"
    Predicate = "This is a Predicate"
    Object = "This is an Object"
}, @{
    Subject = "Cup"
    Predicate = "Runs"
    Object = "Over"
}   | ConvertFrom-Hashtable

 
Show {
        DataGrid -Name lstResults  -columns {
                DataGridTextColumn -Header "Subject" -Binding { Binding -Path "Subject" }
                DataGridTextColumn -Header "Predicate" -Binding { Binding -Path "Predicate" }
                DataGridTextColumn -Header "Object" -Binding { Binding -Path "Object" }
       }
} -ItemsSource $data

 

I get

Show-UI : Index operation failed; the array index evaluated to null.
At line:22 char:5
+ Show <<<<  {
    + CategoryInfo          : NotSpecified: (System.Manageme...n.ScriptBlock[]
   :ScriptBlock[]) [Show-UI], CmdletInvocationException
    + FullyQualifiedErrorId : Error creating WPF Content,PoshWpf.ShowUICommand

May 15, 2011 at 5:47 PM

Sorry found the solution

Show-UI -Width 1000 -Height 725 -WindowStartupLocation CenterScreen {
        DataGrid -Name lstResults  -columns {
                DataGridTextColumn -Header "Subject" -Binding { Binding -Path "Subject" }
                DataGridTextColumn -Header "Predicate" -Binding { Binding -Path "Predicate" }
                DataGridTextColumn -Header "Object" -Binding { Binding -Path "Object" }
        } -ItemsSource $data
 }

Coordinator
May 16, 2011 at 3:16 PM
Edited May 16, 2011 at 3:18 PM

Nice work :-)

One thing we're definitely working on is making sure the ItemsSource and even Resources are settable on the root Show-UI, not just inside.