views:

155

answers:

2

I've got a textbox where I have this: <KeyBinding Command="{Binding MyCommand}" Key="Tab"/>

Problem is it swallows the Tab and doesn't tab to the next control. How can I trap the Tab for the textbox and still preserve tabbing to the next control in the tab order? Edit: I'm also using MVVM and MyCommand is in the ViewModel code, so that's where I need to re-throw the Tab.

A: 

I cannot find a way to set focus to a control given your question as a purely XAML solution.
I choose to create an attacted property and then through binding set the focus to next control from the Command associated with your KeyBinding in the ViewModel.

Here is the View:

<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>

Here is the 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");
      }
    }
  }
}

Here is the code to define the attached property that I found in the following 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. 
      }
    }
  }
}
Zamboni
A: 

Why don't you just use this code in your command handler?

private void MyCommandHandler(){

    // Do command's work here

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

}

That's basically what 'Tab' does, so if you do the same, you're good to go. (Of course reverse the direction if you have a command with Shift-Tab.

I actually wrapped this into an extension method like so...

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);

    }

}

...meaning the prior code becomes even simpler, like this...

private void MyCommandHandler(){

    // Do command's work here

    Control.MoveFocus();

}

...and if you don't know what the currently focused control is, you can just do this...

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

Hope this helps! If so, much appreciated if you vote me up or mark it as accepted!

MarqueIV