c# - 如何从 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;”的页面本身,我不想专注于任何事情。在构造函数中。显然,我错过了一些东西。谁能告诉我它是什么?
解决方案
改为使用Keyboard.KeyDown
。那应该可以解决您的问题。
<Window ...
Keyboard.KeyDown="Page_KeyDown">
...
</Window>
推荐阅读
- mysql - mysql docker容器以代码1退出
- windows - 以编程方式获取 Windows 中所有已安装更新的列表
- c++ - 使用 ODBC 从 Sql Server 获取大数据 - varchar、varbinary
- java - 当对象列表从视图(jsp)传递到控制器时附加方括号
- angular - 环境配置('ng' 不被识别为内部或外部命令。)
- python - 如何在 Superset 上添加 InfluxDB
- go - 我怎样才能建立一个集合?
- c# - 返回动态对象会引发运行时错误
- intellij-idea - 如何打开 Intellij IDEA api 文档侧面板
- vb.net - 枚举更新的列表