Silverlight Canvas doesn't support KeyDown?

非 Y 不嫁゛ 提交于 2019-12-10 15:34:42

问题


I have this:

<Canvas x:Name="LayoutRoot" KeyDown="LayoutRoot_KeyDown">
</Canvas>

In a newly-minted Ag 3 application in VS2008. I simply changed the default <Grid /> to <Canvas /> and added a KeyDown handler that pops a MessageBox.

But no matter how I tried, the KeyDown event just would never, ever fires.

Is it simply that <Canvas /> doesn't support KeyDown or am I doing something wrong?


回答1:


You need to have at least something inside the Canvas that can receive focus, and you will find that the event will bubble up.

You may also want to set IsHitTestVisible="True" on the Canvas.




回答2:


Since your Canvas is the "LayoutRoot" you could use the containing UserControl instead.

Place the property IsTabStop="true" on the UserControl. In the Loaded event call this.Focus(). Attach your KeyDown handler to the UserControl instead of the Canvas.

As long as the plugin itself has the focus (which you can ensure with a little Javascript) you should now get key events.



来源:https://stackoverflow.com/questions/2596149/silverlight-canvas-doesnt-support-keydown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!