首页 > 解决方案 > 如何从 WPF 页面触发 KeyDown 事件

问题描述

目标:从 WPF 页面,我希望用户能够按下预定的键序列,这些键将触发导致控件(按钮)出现的事件。我已经查看了所有我能找到的 KeyDown 和 Keyboard.KeyDown 事件,它们似乎没有在后面的代码中触发事件。

XAML:

<Page x:Class="KioskClient.Begin"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" 
xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors" 
xmlns:local="clr-namespace:KioskClient"
xmlns:uc="clr-namespace:KioskClient"

xmlns:controls="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

mc:Ignorable="d" 
d:DesignHeight="450" d:DesignWidth="800"    
Background="Black" 
Title="SIMS Check In"
Keyboard.KeyDown="Page_KeyDown"
>

<DockPanel LastChildFill="True" Background="Black">

<!--Since you always want the Back, Next, Cancel button to be docked to 
the bottom of the screen, put them first in the Dockpanel and dock them, 
the other objects then fill the space.-->
<controls:UniformGrid Margin="100" Rows="1"
HorizontalAlignment="Center"
VerticalAlignment="Bottom" Background="Black" DockPanel.Dock="Bottom">
</DockPanel>
</Page>

C#:

私人无效Page_KeyDown(对象发送者,KeyEventArgs e)

一旦我可以触发事件但事件没有触发,我就知道该怎么做。除了我设置“this.Focusable = true;”的页面本身,我不想专注于任何事情。在构造函数中。显然,我错过了一些东西。谁能告诉我它是什么?

标签: c#wpfkeydown

解决方案


改为使用Keyboard.KeyDown。那应该可以解决您的问题。

<Window ...
    Keyboard.KeyDown="Page_KeyDown">
    ...
</Window>

推荐阅读