如何在UWP APP中处理KeyDown与Grid的交互 [英] How to handle KeyDown interaction with Grid in UWP APP

查看:103
本文介绍了如何在UWP APP中处理KeyDown与Grid的交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我想创建一个用户控件,并允许用户键入 上/下/左/右箭头键来移动控件的位置。我不知道如何使它工作。

I want to create an user control and enable user to type Up/Down/Left/Right arrow key to move the control's position. I don't know how to make it work.

用户控制简单代码如下:

The user control simple code like bellow:

<UserControl
    x:Class="UserControlDemo.SignatureFieldControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:SignatureDialogCollection.Drawing"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="80"
    d:DesignWidth="200" Width="150" Height="50" MinWidth="50" MinHeight="25">

    <Grid x:Name="rootGrid" Background="#233038" KeyUp="rootGrid_KeyUp">
        <TextBlock x:Name="txtType" HorizontalAlignment="Center" VerticalAlignment="Center" Width="100" Height="30" 
                   TextAlignment="Center" FontSize="20" Foreground="White">Sign</TextBlock>
    </Grid>
</UserControl>


using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace UserControlDemo
{
    public sealed partial class SignatureFieldControl : UserControl
    {
        public SignatureFieldControl()
        {
            this.InitializeComponent();
        }

        private void rootGrid_KeyUp(object sender, KeyRoutedEventArgs e)
        {
           //todo
        }
    }
}

似乎rootGrid_KeyUp事件永远不会被解雇。

It seems the rootGrid_KeyUp event never be fired.

如果我有两个或更多SignatureFieldControl在页面上,我想知道哪一个被选中,但我不知道哪一个是关注的。

If I have two or more SignatureFieldControl on the page, I want to know which one is selected, but I don't know which one is focused.

最好的问候

.Net Windows C#

.Net Windows C#




推荐答案

常见的初学者错误是不理解键盘事件与具有焦点的窗口相关联。这里的相关性是TextBlock控件是只读的,因此它们不处理键盘事件。据我所知,网格控件不能
获得焦点,因此它也不会处理键盘事件除非事件来自子控件。如果你将TextBlock更改为TextBox,那么它将更好。

A common beginner mistake is to not understand that keyboard events are associated with a window with focus. The relevance of that here is that TextBlock controls are read-only so they don't process keyboard events. The Grid control, as far as I know, cannot get focus so it also does not process keyboard events unless the event is from a child control. If you change the TextBlock to a TextBox then it will work better.

将其更改为TextBox然后为每个SignatureFieldControl赋予一个名称,然后在rootGrid_KeyUp中使用以下内容:

Change it to a TextBox then give a name to each of the SignatureFieldControls then use the following in rootGrid_KeyUp:

Grid g = sender as Grid;
if (g == null)
    return;
SignatureFieldControl me = g.Parent as SignatureFieldControl;
if (me == null)
    return;
System.Diagnostics.Debug.WriteLine("rootGrid_KeyUp for {0}, key {1}", me.Name, e.Key);

当我测试我得到一些"Scroll"时我不进入自己的钥匙。因此,您可能需要添加忽略它们的代码。

When I test that I get some "Scroll" keys that I don't enter myself. So you might need to add code that ignores them.


这篇关于如何在UWP APP中处理KeyDown与Grid的交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆