您可以使用PreviewTextInput事件来实现这一点。此事件在输入框中输入文本时触发,无论是用户手动输入还是使用扫码枪输入。

在输入框中添加PreviewTextInput事件处理程序,并将其绑定到一个名为OnPreviewTextInput的方法。在该方法中,检查输入的字符是否为回车键并调用OnPreviewKeyDown方法。

以下是您可以尝试的示例代码:

<TextBox x:Name='inputBox' Margin='46,-20,0,0' Width='100' Height='22' FontFamily='./#仿宋_GB2312' FontWeight='DemiBold' FontStretch='Medium' Foreground='Black' HorizontalAlignment='Left' VerticalAlignment='Top' PreviewTextInput='OnPreviewTextInput' Grid.ColumnSpan='2' />
private void OnPreviewTextInput(object sender, TextCompositionEventArgs e)
{
    if (e.Text == '\r')
    {
        OnPreviewKeyDown(sender, new KeyEventArgs(Keyboard.PrimaryDevice, PresentationSource.FromVisual((Visual)sender), 0, Key.Enter));
    }
}

当用户使用扫码枪将条码的内容放到输入框中后,会触发PreviewTextInput事件,由于输入的内容是回车符,因此会执行OnPreviewKeyDown方法,从而实现自动调用功能。

WPF TextBox 扫码枪输入自动触发事件

原文地址: http://www.cveoy.top/t/topic/oUC5 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录