为什么在 PowerShell 中使用文本框“文本”时 WPK 数据绑定不起作用?财产?
再会。我想问一个问题。 为什么此代码中的 TextBox 控件“Txt”没有接收 $CounterObject 对象的属性值?
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您单独查看 Get-PowerShellDataSource 的输出,您将看到:
1. 您的脚本不起作用(没有输出)。
2. 修复脚本时(见下文),可以在属性“Output”中找到输出(作为数组)。
因此,如果将数据绑定路径更改为“Output[0].CounterValue”,它将起作用。
您也不需要 onLoaded 事件处理程序。
这段代码有效:
希望有帮助!
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!