Silverlight Keydown事件不会为箭头键触发 [英] silverlight keydown event doesn't fire for arrow keys

查看:80
本文介绍了Silverlight Keydown事件不会为箭头键触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在scrollview中有一个画布.我将keydown事件处理程序附加到scrollview.对于大多数键,将调用处理程序.

I have a canvas inside a scrollview. I attached a keydown event handler to the scrollview. For most keys, the handler gets called.

但是,对于箭头键,不会调用处理程序.相反,滚动视图会沿适当的方向滚动.

However, for the arrow keys, the handler does not get called. Instead, the scrollview gets scrolled in the appropriate direction.

我还在滚动视图上附加了一个keyup处理程序,并且确实会为箭头键调用keyup.

I also attached a keyup handler to the scrollview and the keyup does get called for the arrow keys.

有什么办法可以在这里触发向下箭头事件吗?

Is there any way to get the arrow key down event here?

推荐答案

我发现了这个愚蠢的技巧,可以使其正常工作.将scrollview设置为不是制表符,可以防止它吃关键事件..但是然后我在页面上又有了一个文本框,因为滚动视图不再存在,所以突然间总有焦点.因此,我通过使不可见的文本框获得焦点来解决了该问题.

I found this silly hack to make it work. Setting the scrollview to not be a tabstop keeps it from eating the key events.. but then I had another textbox on the page that all of a sudden ALWAYS had focus because the scrollview didn't anymore. So I fixed that by letting an invisible textbox get focus.

scrollView.IsTabStop = false;

invisibleTextBox.Foreground = new SolidColorBrush(Colors.Transparent);
invisibleTextBox.Background = new SolidColorBrush(Colors.Transparent);
Canvas.SetZIndex(invisibleTextBox, -1000);
invisibleTextBox.KeyDown += new KeyEventHandler(HandleKeyDown);
invisibleTextBox.KeyUp += new KeyEventHandler(HandleKeyUp);

我还不得不将文本框从画布上移开,因为尽管它不可​​见,但其轮廓仍然显示出来.

I also had to move the text box off the canvas because despite being invisible, its outline still showed up.

第二次我使用了一个文本框,因为这是我发现的第一项可以捕获KeyDown事件的东西.但是,UserControl可以.因此,最好使用UserControl而不是不可见的文本框.您可以根据需要在UserControl上调用Focus().

2nd I used a textbox because that was the first thing I found that could capture KeyDown events. However, a UserControl can. So it would probably be better practice to use a UserControl instead of an invisible text box. You can call Focus() on the UserControl if needed.

这篇关于Silverlight Keydown事件不会为箭头键触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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