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

mik*_*ike 5 c# wpf key-bindings mvvm

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

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

Zam*_*oni 0

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

这是视图:

<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>
Run Code Online (Sandbox Code Playgroud)

这是视图模型:

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");
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

这是我在以下答案中找到的定义附加属性的代码。

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. 
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)