如何模拟键盘上的WPF树视图按下F2按钮? [英] How to simulate pressing F2 button with keyboard focus on treeview in wpf?

查看:157
本文介绍了如何模拟键盘上的WPF树视图按下F2按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  //发送
的密钥var key = Key.F2;
//发送
的事件var routedEvent = Keyboard.KeyDownEvent;
TreeControl.RaiseEvent(
new KeyEventArgs(
Keyboard.PrimaryDevice,
PresentationSource.FromVisual(TreeControl),
0,
key){RoutedEvent = routedEvent }
);

但是我不能用这种方式以编程方式模拟F2,但是 TreeControl 具有键盘焦点,如果手动按F2后立即可以正常工作。请帮忙。

我试图在 OnItemSelected 处理程序被调用的时候调用这个代码(在必要时并不总是)。

解决方案

最初发布的代码是正确的,我发现它模拟F2树视图正确按下。



当我尝试模拟F2点击 OnItemSelected 时,处理程序它不起作用,但从 LayoutUpdated 处理程序工作正常。

所以树视图需要完全更新为准备好接受按键。


I am trying the following:

// Key to send   
 var key = Key.F2;       
// Event to send                         
var routedEvent = Keyboard.KeyDownEvent; 
TreeControl.RaiseEvent(
               new KeyEventArgs(
                Keyboard.PrimaryDevice,
                PresentationSource.FromVisual(TreeControl),
                0,
                key) { RoutedEvent = routedEvent }
            );

But I can't simulate F2 programmatically this way though TreeControl has keyboard focus and if I press F2 immediately after manually it works fine. Please help.

I am trying to call this code in the moment OnItemSelected handler is called (not always, when necessary).

解决方案

Initially posted code is right and I figured out that it simulates F2 press correctly for treeview. Just another problems not related to this prevented me from seeing this.

When I tried to simulate F2 click from OnItemSelected handler it didn't work but the same from LayoutUpdated handler works fine.

So the treeview needs to be fully updated to be ready to accept key presses.

这篇关于如何模拟键盘上的WPF树视图按下F2按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆