移动焦点以响应XAML中的键盘事件 [英] Move focus in response to keyboard events in XAML

查看:142
本文介绍了移动焦点以响应XAML中的键盘事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文本框的WPF视图。我想自动将焦点从第一个文本框移动到第二个,当用户按下Tab键时,键盘上的向下箭头。



看起来像我应该可以做到这一点100%声明,但由于某种原因,我认为这样做的命令似乎并没有做任何事情。这是我第一次尝试不起作用:

 < StackPanel> 
< TextBox Text =Test>
< TextBox.InputBindings>
<! - 我意识到ComponentCommands.MoveFocusDown不起作用...
这只是我尝试过的一个例子,我正在寻找的类型
的答案 - >
< KeyBinding Key =DownCommand =ComponentCommands.MoveFocusDown/>
< /TextBox.InputBindings>
< / TextBox>
< TextBox>< / TextBox>
< / StackPanel>

有没有人有这方面的经验?似乎我应该能够使用InputBindings或一个EventTrigger来做到这一点。

我使用MVVM,这是一个查看关注。我可以在一个简单的代码隐藏(作为一个视图的关注,这是合理的),但它只是感觉就像我失去了一些东西。

解决方案我希望有人能拿出更优雅的东西来说明这一点,但这是我迄今为止所做的。这不是100%的XAML,但至少是通用的。

这个例子显示了一个带有两个按钮和两个文本框的窗口。向下箭头循环它们之间的焦点。



我希望这有助于您。

 < Window x:Class =WPF_Playground.Window1
xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x =http://schemas.microsoft.com/ winfx / 2006 / xaml
Title =Window1Height =300Width =300
>
< Window.CommandBindings>
< CommandBinding Command =ComponentCommands.MoveFocusDownExecuted =CommandBinding_Executed/>
< /Window.CommandBindings>
< StackPanel KeyboardNavigation.DirectionalNavigation =Cycle>
<按钮>测试器< /按钮>
<按钮> Tester2< /按钮>
< TextBox Text =Test>
< TextBox.InputBindings>
< KeyBinding Command =ComponentCommands.MoveFocusDownGesture =DOWN/>
< /TextBox.InputBindings>
< / TextBox>
< TextBox Text =Test2>
< TextBox.InputBindings>
< KeyBinding Command =ComponentCommands.MoveFocusDownGesture =DOWN/>
< /TextBox.InputBindings>
< / TextBox>
< / StackPanel>
< / Window>

事件处理程序(根本没有错误处理):

  private void CommandBinding_Executed(object sender,ExecutedRoutedEventArgs e)
{
UIElement senderElement = sender as UIElement;
UIElement focusedElement = FocusManager.GetFocusedElement(senderElement)as UIElement;
bool result = focusedElement.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
Debug.WriteLine(result);
}


I've got a WPF view with two textboxes. I'd like to automatically move focus forward from the first textbox to the second when the user hits the down arrow on the keyboard exactly like Tab does.

It seems like I ought to be able to do this 100% declaratively, but for some reason the commands I thought would do this don't seem to do anything. Here is my first attempt that doesn't work:

<StackPanel>
    <TextBox Text="Test">
        <TextBox.InputBindings>
            <!-- I realize ComponentCommands.MoveFocusDown doesn't work...
                 This is just an example of what I've tried and the type
                 of answer I'm looking for -->
            <KeyBinding Key="Down" Command="ComponentCommands.MoveFocusDown" />
        </TextBox.InputBindings>
    </TextBox>
    <TextBox></TextBox>
</StackPanel>

Does anyone have any experience with this? Seems like I ought to be able to use either InputBindings or an EventTrigger to do this.

I'm using MVVM and this is a View concern. I could just drop in a little codebehind (being a view concern, this is reasonable), but it just feels like I'm missing something.

解决方案

I hope someone comes up with something more elegant that this, but this is what I have so far. It is not 100% XAML, but it is at least generic.

This example shows a window with two buttons and two text boxes. The down arrow cycles the focus between them.

I hope this helps.

<Window x:Class="WPF_Playground.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300"
    >
    <Window.CommandBindings>
        <CommandBinding Command="ComponentCommands.MoveFocusDown" Executed="CommandBinding_Executed"/>
    </Window.CommandBindings>
    <StackPanel KeyboardNavigation.DirectionalNavigation="Cycle">
        <Button>Tester</Button>
        <Button>Tester2</Button>
        <TextBox Text="Test">
            <TextBox.InputBindings>
                <KeyBinding Command="ComponentCommands.MoveFocusDown" Gesture="DOWN" />
            </TextBox.InputBindings>
        </TextBox>
        <TextBox Text="Test2">
            <TextBox.InputBindings>
                <KeyBinding Command="ComponentCommands.MoveFocusDown" Gesture="DOWN" />
            </TextBox.InputBindings>
        </TextBox>
    </StackPanel>
</Window>

The event handler (no error handling at all):

private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
    UIElement senderElement = sender as UIElement;
    UIElement focusedElement = FocusManager.GetFocusedElement(senderElement) as UIElement;
    bool result = focusedElement.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
    Debug.WriteLine(result);
}

这篇关于移动焦点以响应XAML中的键盘事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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