0

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

1 Answer 1

1

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:

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 Output[0].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" }
        Return $CounterObject
    }
} -asjob

Hope that helps!

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. I'll try this. But where I may found info about "output" variable? There was not declared this var in officail docs.
The easiest way is probably to just look at the output of, say, Get-PowerShellDataSource -Script { "Foo" }. Note that the scriptblock returns "Foo" in this case and that value can also be found in the Output property. There might be easier/better ways to solve this problem. Unfortunately I don't know enough WPK to tell.
Good afternoon. In fact everything works. Thank you. Please tell me that where you learned about an array Output []? At what point of the script and how the object is created?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.