Add-Type -AssemblyName PresentationFramework
Add-Type -AssemblyName PresentationCore
Add-Type -AssemblyName WindowsBase
[xml]$xaml = @"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Name Change" Height="260" Width="800"
x:Name="Window">
<Grid>
<TextBox x:Name="txtUPN" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Margin="32,155,0,0"/>
</Grid>
</Window>
"@
$reader = (New-Object System.Xml.XmlNodeReader $xaml)
$window = [Windows.Markup.XamlReader]::Load($reader)
$upn = $Window.FindName('txtUPN')
$upn.Add_KeyDown(
{if ($_.KeyCode -eq "Enter")
{$upn.Text | Out-Host}})
# KeyDown event fires, but doesn't capture the Enter key
$window.ShowDialog()
使用Powershell和XAML GUI,我有3个不同的文本框,我想使用Enter键来触发函数调用,但是遇到的任何问题似乎都不起作用。是否有我看不见的东西,缺少一些组件?似乎什么都没认出Enter键,我什至在KeyPress上尝试了KeyChar。我对窗体窗口事件不感兴趣,我更喜欢文本框事件。似乎迫使您使用Button。谢谢。
答案 0 :(得分:1)
keycode属性在WinForms中工作正常,但是在WPF中似乎需要使用密钥
$upn.Add_KeyDown(
{if ($_.Key -eq "Enter")
{$upn.Text | Out-Host}})
答案 1 :(得分:1)
Mikel V.是正确的。
我正在寻找此解决方案,因为我正在将脚本从WinForms更新为XAML。正确地将“ .KeyCode”更改为“ .Key”。
$TextBoxInput.Add_KeyDown({
if ($_.Key -eq "Enter") {
#do stuff
}
})