WPF:Tab,Swallows选项卡的KeyBinding并且不传递它

问题描述 投票:5回答:3

我有一个文本框,我有这个:<KeyBinding Command="{Binding MyCommand}" Key="Tab"/>

问题是它吞下Tab并且没有Tab到下一个控件。如何捕获文本框的Tab并仍然将Tab键保留到Tab键顺序中的下一个控件?编辑:我也在使用MVVM和MyCommand在ViewModel代码中,所以我需要重新抛出Tab。

c# wpf mvvm key-bindings
3个回答
0
投票

我找不到一种方法来将焦点设置为控件,因为您的问题纯粹是XAML解决方案。 我选择创建一个attacted属性,然后通过绑定将焦点设置为与ViewModel中与KeyBinding关联的Command的下一个控件。

这是视图:

<Window x:Class="WarpTab.Views.MainView"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:c="clr-namespace:WarpTab.Commands" 
  xmlns:Views="clr-namespace:WarpTab.Views" 
  xmlns:local="clr-namespace:WarpTab.ViewModels" 
  Title="Main Window" Height="400" Width="800">

  <Window.Resources>
      <c:CommandReference x:Key="MyCommandReference" Command="{Binding MyCommand}" />
  </Window.Resources>

  <DockPanel>
    <ScrollViewer>
      <WrapPanel >
        <TextBox Text="First text value" >
            <TextBox.InputBindings>
                <KeyBinding Command="{StaticResource MyCommandReference}" Key="Tab"/>
            </TextBox.InputBindings>
        </TextBox>
        <TextBox Text="Next text value" local:FocusExtension.IsFocused="{Binding FocusControl}"  />
        <Button Content="My Button" />
      </WrapPanel>
    </ScrollViewer>
  </DockPanel>
</Window>

这是ViewModel:

using System.Windows.Input;
using WarpTab.Commands;

namespace WarpTab.ViewModels
{
  public class MainViewModel : ViewModelBase
  {
    public ICommand MyCommand { get; set; }
    public MainViewModel()
    {
      MyCommand = new DelegateCommand<object>(OnMyCommand, CanMyCommand);
    }

    private void OnMyCommand(object obj)
    {
      FocusControl = true;

      // process command here

      // reset to allow tab to continue to work
      FocusControl = false;
      return;
    }

    private bool CanMyCommand(object obj)
    {
      return true;
    }

    private bool _focusControl = false;
    public bool FocusControl
    {
      get
      {
        return _focusControl;
      }
      set
      {
        _focusControl = value;
        OnPropertyChanged("FocusControl");
      }
    }
  }
}

下面是我在以下answer中找到的附加属性的代码。

using System.Windows;

namespace WarpTab.ViewModels
{
  public static class FocusExtension
  {
    public static bool GetIsFocused(DependencyObject obj)
    {
      return (bool)obj.GetValue(IsFocusedProperty);
    }

    public static void SetIsFocused(DependencyObject obj, bool value)
    {
      obj.SetValue(IsFocusedProperty, value);
    }

    public static readonly DependencyProperty IsFocusedProperty =
            DependencyProperty.RegisterAttached(
            "IsFocused", typeof(bool), typeof(FocusExtension),
            new UIPropertyMetadata(false, OnIsFocusedPropertyChanged));

    private static void OnIsFocusedPropertyChanged(DependencyObject d,
            DependencyPropertyChangedEventArgs e)
    {
      var uie = (UIElement)d;
      if ((bool)e.NewValue)
      {
        uie.Focus(); // Don't care about false values. 
      }
    }
  }
}

0
投票

为什么不在命令处理程序中使用此代码?

private void MyCommandHandler(){

    // Do command's work here

    TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Next);
    request.Wrapped = true;
    control.MoveFocus(request);

}

这基本上就是'Tab'所做的,所以如果你这样做,你就会很高兴。 (当然,如果你有一个带有Shift-Tab的命令,则反转方向。

我实际上把它包装成一个像这样的扩展方法......

public static class NavigationHelpers{

    public static void MoveFocus(this FrameworkElement control, FocusNavigationDirection direction = FocusNavigationDirection.Next, bool wrap = true) {

        TraversalRequest request = new TraversalRequest(direction);
        request.Wrapped = wrap;
        control.MoveFocus(request);

    }

}

...意思是先前的代码变得更简单,像这样......

private void MyCommandHandler(){

    // Do command's work here

    Control.MoveFocus();

}

......如果你不知道当前关注的控件是什么,你可以这样做......

(Keyboard.FocusedElement as FrameworkElement).MoveFocus();

希望这可以帮助!如果是这样,非常感谢您投票给我或将其标记为已接受!


0
投票

遇到同样的问题,碰到了这个帖子,花了一些时间才找到最好的答案。参考:Use EventTrigger on a specific key定义此类:

using System; using System.Windows.Input; using System.Windows.Interactivity;

public class KeyDownEventTrigger : EventTrigger
{

    public KeyDownEventTrigger() : base("KeyDown")
    {
    }

    protected override void OnEvent(EventArgs eventArgs)
    {
        var e = eventArgs as KeyEventArgs;
        if (e != null && e.Key == Key.Tab)
        { 
            this.InvokeActions(eventArgs);                
        }
    }
}

文本框的xaml:

<TextBox x:Name="txtZip"
     Text="{Binding Zip, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<TextBox.InputBindings>
    <KeyBinding Key="Enter" Command="{Binding ZipLookup.GetAddressByZipKeyCommand}" CommandParameter="{Binding ElementName=txtZip, Path=Text}" />
</TextBox.InputBindings>
<i:Interaction.Triggers>
    <iCustom:KeyDownEventTrigger EventName="KeyDown">
        <i:InvokeCommandAction Command="{Binding ZipLookup.GetAddressByZipKeyCommand}" CommandParameter="{Binding ElementName=txtZip, Path=Text}" />
    </iCustom:KeyDownEventTrigger>
</i:Interaction.Triggers>
</TextBox>

在您的窗口或用户控件根标记中包含以下属性:

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:iCustom="clr-namespace:[NAMESPACE FOR CUSTOM KEY DOWN CLASS]"
© www.soinside.com 2019 - 2024. All rights reserved.