尝试通过raiseEvent连接到textBox -但我在textBox文本中看不到键值。我可以看到textbox事件"OnKeyDownEvent“在断点处停止了--但是我不明白为什么KeyEventArgs ( Key.D0 )的文本没有插入到textbox文本中。
代码:
if( currentTextBoxInFocus != null )
{
KeyEventArgs k = new KeyEventArgs( Keyboard.PrimaryDevice, Keyboard.PrimaryDevice.ActiveSource, System.Environment.ProcessorCount, Key.D0 );
//KeyEventArgs k = new KeyEventArgs( Keyboard.PrimaryDevice, Keyboard.PrimaryDevice.ActiveSource, 0, Key.D0 );
k.RoutedEvent = UIElement.KeyDownEvent;
currentTextBoxInFocus.RaiseEvent( k );
k.RoutedEvent = UIElement.KeyUpEvent;
currentTextBoxInFocus.RaiseEvent( k );
}发布于 2010-12-15 10:37:38
您正在尝试使用按键事件作为输入源,而它们实际上用于指示来自操作系统的键盘输入的结果。这些事件不会导致将文本添加到TextBox。相反,它们指示何时将键盘输入输入到控件(如TextBox)中。
如果您想从代码模拟输入文本,只需将所需的文本添加到TextBox属性中:
int caret = currentTextBoxInFocus.CaretIndex;
currentTextBoxInFocus.Text = String.Format("{0}0{1}", currentTextBoxInFocus.Text.Substring(0, caret), currentTextBoxInFocus.Text.Substring(currentTextBoxInFocus.CaretIndex));
currentTextBoxInFocus.CaretIndex = caret + 1;https://stackoverflow.com/questions/4441010
复制相似问题