在Powershell中运行的XAML文本框中捕获输入密钥

问题描述 投票:1回答:1
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。谢谢。

wpf powershell xaml events keydown
1个回答
0
投票

keycode属性在WinForms中工作正常,但在WPF中似乎需要使用密钥

$upn.Add_KeyDown(
{if ($_.Key -eq "Enter") 
{$upn.Text | Out-Host}})
© www.soinside.com 2019 - 2024. All rights reserved.