Good day. I’d like to ask a question.
Why TextBox control “Txt” in this code does not receive a property value of $CounterObject object?
New-Grid -Height 150 -Width 200 -Rows 3 {
New-Label -Name InfoLabel -Row 0 "Some message"
New-TextBox -Name Txt -Row 1 -DataBinding @{ Text = New-Binding -Path CounterValue -Mode OneWay}
New-Button -Name B1 -Row 2 -Width 100 "OK"
} -DataContext {
Get-PowerShellDataSource -Script {
$CounterObject = New-Object -TypeName PSObject -Property @{ CounterValue = "Some Text" }
ForEach-Object {
$_.CounterValue
}
}
} -on_Loaded {
Register-PowerShellCommand -Run -Once -ScriptBlock {
$window.Content.DataContext.Script = $window.Content.DataContext.Script
}
} -asjob
If you look at the output of Get-PowerShellDataSource separately you will see that:
1. Your script doesn’t work (there is no output).
2. When you fix the script (see below), the output can be found in property “Output” (as an array).
So if you change the databinding path to “Output[0].CounterValue”, it will work.
You also don’t need the onLoaded event handler.
This code works:
Hope that helps!