我有一个文本框,当输入的键是“return”时,它有 keydown 事件 我有一个条形码阅读器,可以将文本读入其中,但它不会写入多个键,即只写了一个字母,让我们说“a”,如果我写第二个字母“a”被覆盖成为“b”但没有成为“ab”。有谁知道这是什么原因?
private void barcodetexbox_KeyDown(object sender, KeyEventArgs e)
{
if (scannedString.Text != "" && e.Key==Key.Return)
{
//do something
}
}
在“MainWindow.xaml”中
<TextBox x:Name="scannedString" HorizontalAlignment="Left" Height="50"
Margin="468,164,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="450"
FontSize="24" Focusable="True" Padding="0,6,0,0"
KeyDown="barcodetexbox_KeyDown" />
最佳答案
KeyDown 事件旨在让您知道某个时刻按下了哪些键,并且您的条形码阅读器似乎模拟了键盘,因此您需要连接它发送的字符
在您的 Key_Down 事件中,您必须执行类似的操作:
this.scannedString += e.Key;
当你看到返回时:
barcodeTextBox.Text = this.scannedString;
关于c# - WPF 文本框中的按下键只允许一个字符事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14562985/