5

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.

mike
  • 121
  • 2
  • 4
  • 1
    Maybe the `LostFocus` event is more suitable for your purposes? (I still considerer your question to be of interest, so +1.) – Heinzi Aug 10 '10 at 15:25
  • I looked into LostFocus, but with my edit of I'm using MVVM, I'd like not put code there. – mike Aug 10 '10 at 15:29
  • I'm think more about the LostFocus,how could I bind the Command to the LostFocus event? I want t odo this without putting code into the xaml's code behind, only binding to a command in my ViewModel. – mike Aug 10 '10 at 17:50
  • You'd have to use CodeBehind for LostFocus. I understand your concern; however, sometimes a single line of code behind is better and more maintainable than lots of MVVM binding code and workarounds... – Heinzi Aug 11 '10 at 10:27

4 Answers4

1

It's easy to achieve, just don't use KeyBinding for this. Handle your TextBox's OnKeyDown event:

<TextBox KeyDown="UIElement_OnKeyDown" ...

Then on the code-behind, execute your command whenever Tab is pressed. Unlike KeyBinding, this won't swallow the TextInput event so it should work.

    private void OnKeyDown(object sender, KeyEventArgs e)
    {
        switch (e.Key)
        {
            case Key.Tab:
                // Execute your command. Something similar to:
                ((YourDataContextType)DataContext).MyCommand.Execute(parameter:null);
                break;
        }
    }
daniloquio
  • 3,822
  • 2
  • 36
  • 56
0

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. 
      }
    }
  }
}
Community
  • 1
  • 1
Zamboni
  • 7,897
  • 5
  • 43
  • 52
0

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!

Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
0

Had the same problem, came across this thread and took me a while to find the best answer. Reference: Use EventTrigger on a specific key Define this class:

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

The xaml for your text box:

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

In your window or user control root tag include these attributes:

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:iCustom="clr-namespace:[NAMESPACE FOR CUSTOM KEY DOWN CLASS]"
EmanP
  • 306
  • 1
  • 2
  • 14