开发者

Silverlight Canvas doesn't support KeyDown?

I have this:

<Canvas x:Name="LayoutRoot" KeyDown="LayoutRoot_KeyDown">
&l开发者_JAVA百科t;/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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜