title: Keybinding a TextBox using XAML and WPF in C#
body: <p>I'm trying to bind to a key event in a WPF UserControl.  The component is a TextBox and my XAML is</p>
  
  <pre><code>&lt;TextBox Name="textBarCode" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="0,0,10,0"  Width="300"&gt;
      &lt;TextBox.InputBindings&gt;
          &lt;KeyBinding Key="Enter" Command="{Binding ImportPanel.BarcodeTextEnterKeyCommand}"/&gt;
          &lt;KeyBinding Key="Tab" Command="{Binding ImportPanel.BarcodeTextTabKeyCommand}"/&gt;
      &lt;/TextBox.InputBindings&gt;
  &lt;/TextBox&gt;
  </code></pre>
  
  <p>I'm not sure if it is needed or not but the namespace declaration is</p>
  
  <pre><code>&lt;UserControl x:Class="Scimatic.Samples.Actions.ImportPanel"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:ig="http://schemas.infragistics.com/xaml" 
      xmlns:components="clr-namespace:Scimatic.Mondrian.Components;assembly=Mondrian"
      xmlns:sampleInventory="clr-namespace:Scimatic.Mondrian.Views.SampleInventory;assembly=Mondrian"
      xmlns:trackingTags="clr-namespace:Scimatic.Mondrian.Views.TrackingTags;assembly=Mondrian"&gt;
  </code></pre>
  
  <p>The code that declares the command in the underlying xaml.cs file is </p>
  
  <pre><code>_barcodeKeyCommand = new ActionCommand(() =&gt;
  {
      if (!_parent &amp;&amp; FocusableTagOnBarcode != null)
      {
          trackingInfo.SetFocusOnTag(FocusableTagOnBarcode);
      }
      else
      {
      buttonImport.Focus();
      }
  });
  </code></pre>
  
  <p>The code that sets these properties is:</p>
  
  <pre><code>/// &lt;summary&gt;
  /// The command property for the enter key in the barcode text box
  /// &lt;/summary&gt;
  public ICommand BarcodeTextTabKeyCommand
  {
      get
      {
          return _barcodeKeyCommand;
      }
  }
  
  The commands are returned in the same class using the same method:
  
  /// &lt;summary&gt;
  /// The command property for the enter key in the barcode text box
  /// &lt;/summary&gt;
  public ICommand BarcodeTextEnterKeyCommand
  {
      get
      {
          return _barcodeKeyCommand;
      }
  }
  </code></pre>
  
  <p>However no matter what I try (and I've tried all kinds of things); I just cannot get the command to be called.  I've clearly done something wring but could someone please help me.  I'm fairly new to C# and I've wasted two days trying to respond to an enter key in a text box!</p>
  
  <p>Thank you in advance,</p>
  
  <p>Regards,</p>
  
  <p>Neil</p>