开发者

WPF WindowsFormsHost does not bubble up untreated key gestures

Ok, I'm not sure I understand how this should work, but in my app I have bound a key gesture (Ctrl+K) to a RoutedCommand. No matter where I focus in my app, the key combination works, except if I use it inside of a WindowsFormsHost containing a web browser control. I tried capturing the PreviewKeyDown event for the web browser control and s开发者_Go百科etting the IsInputKey to false. This should, in theory, cause the Host to consider the key gesture untreated and send it upwards, but that doesn't happen.

UPDATE:

If I override the control's bool IsInputKey (Keys keyData) and return false, it works and the command gets executed as it should.

Maybe there is something wrong in the way I handled PreviewKeyDown?

private void browser_PreviewKeyDown (object sender, PreviewKeyDownEventArgs e)
{
    e.IsInputKey = false;
}

I expected this to allow me to capture any key combination from my main window. I'd rather fix this with event handling than extending a control...


If I understand it correctly you want to prevent that the keystroke is handled by the hosted Winform. However, your example code indicates that you try to handle it inside the hosted control.

For handling tunneling events you better handle it "outside" the hosted control, in the WPF visual tree. You can - for example - handle it as follows:

<Grid PreviewKeyDown="Grid_PreviewKeyDown" PreviewMouseDown="Grid_PreviewMouseDown" Height="250" Width="250">
    <WindowsFormsHost Name="windowsFormsHost1">
        <wf:Form1 TopLevel="False" />
    </WindowsFormsHost>
</Grid>

By setting the event as handled as follows, it prevents that the event is propagated further down the visual tree:

private void Grid_PreviewKeyDown(object sender, KeyEventArgs e)
{
    e.Handled = true;
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜