wpf KeyDown 事件未从网格引发
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15241118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
KeyDown event not raising from a grid
提问by Dork
Here I have sample window with a grid. I need to capture event when key is pressed. But it is not raising when I click grid area and then press key. It will work only if Textbox is focused. I know it will work if I capture it from Window. But I have other application with few usercontrols and I need to capture it from distinct ones. I tried to set Focusable.false for Window and true for Grid but it not helps. Any solutions?
在这里,我有一个带网格的示例窗口。我需要在按下键时捕获事件。但是当我单击网格区域然后按键时它不会升高。仅当 Textbox 处于焦点时才有效。我知道如果我从 Window 捕获它会起作用。但是我有其他用户控件很少的应用程序,我需要从不同的应用程序中捕获它。我试图为 Window 设置 Focusable.false ,为 Grid 设置 true ,但它没有帮助。任何解决方案?
<Window x:Class="Beta.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Closed="Window_Closed_1" Focusable="False">
<Grid KeyDown="Grid_KeyDown_1" Focusable="True">
<TextBox x:Name="tbCount" HorizontalAlignment="Left" Height="35" Margin="310,49,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="83"/>
</Grid>
采纳答案by Ouarzy
Right this is weird. This is clearly a focus problem, still I can not understand why the grid do not take Focus, even when we click on it.
没错,这很奇怪。这显然是一个焦点问题,我仍然不明白为什么网格不带焦点,即使我们点击它。
Though there is a workaround: create an handler for the loaded event of the grid:
虽然有一个解决方法:为网格的加载事件创建一个处理程序:
<Grid x:Name="theGrid" KeyDown="Grid_KeyDown_1" Focusable="True" Loaded="TheGrid_OnLoaded">
And then force focus in your code behind:
然后强制将焦点放在后面的代码中:
private void TheGrid_OnLoaded(object sender, RoutedEventArgs e)
{
theGrid.Focus();
}
Your keydown event will work after that. Hope it helps.
之后您的 keydown 事件将起作用。希望能帮助到你。
回答by Nagev
I had the same issue with a Universal Windows Platform (UWP) app. I attached the event to a grid in XAML but it would only work when the focus was on the TextBox. I found the answer (not just a workaround) on MSDN: https://social.msdn.microsoft.com/Forums/en-US/56272bc6-6085-426a-8939-f48d71ab12ca/page-keydown-event-not-firing?forum=winappswithcsharp
我在通用 Windows 平台 (UWP) 应用程序中遇到了同样的问题。我将事件附加到 XAML 中的网格,但它仅在焦点位于 TextBox 上时才有效。我在 MSDN 上找到了答案(不仅仅是解决方法):https: //social.msdn.microsoft.com/Forums/en-US/56272bc6-6085-426a-8939-f48d71ab12ca/page-keydown-event-not-firing ?forum=winappswithcsharp
In summary, according to that post, the event won't fire because when focus to the TextBox is lost, it's passed higher up so the Grid won't get it. Window.Current.CoreWindow.KeyDownshould be used instead. I've added my event handlers to the page loaded event like this:
总之,根据那篇文章,该事件不会触发,因为当失去对 TextBox 的焦点时,它会向上传递,因此 Grid 不会得到它。Window.Current.CoreWindow.KeyDown应该改用。我已将我的事件处理程序添加到页面加载事件中,如下所示:
private void Page_Loaded(object sender, RoutedEventArgs e)
{
Window.Current.CoreWindow.KeyDown += coreWindow_KeyDown;
Window.Current.CoreWindow.KeyUp += CoreWindow_KeyUp;
}
This works as expected for me.
这对我来说按预期工作。
回答by joseph taylor
I tried using the Focus Method too, to no avail, until I set the Focusable property to true ( It was default to False. )
我也尝试使用 Focus Method,但无济于事,直到我将 Focusable 属性设置为 true(默认为 False。)

