title: Silverlight KeyDown event in behavior
body: <p>In my Silverlight 4 <code>DataGrid</code> control, I wanted to attach a very simple Behavior which executes a custom command on key Press - actually, commit the selected item in the DataGrid on ENTER key press.</p>
  
  <p>While the Behavior actually works (see my code...</p>
  
  <pre><code>//.... in "OnAttached()..."
  this.AssociatedObject.AddHandler(Control.KeyDownEvent, new KeyEventHandler(OnKeyDown), true);
  
  private void OnKeyDown(object sender, KeyEventArgs e)
      {
          if (e.Key == Key.Enter)
          {
              InvokeCommand();
          }
      }
  </code></pre>
  
  <p>...) I have the problem, that the DataGrid seems to handle the ENTER key press itself and proceeds to the next row. Obviously, what happens is that the wrong Row is committed, because when I handle the Key Press, the row selectedion has already changed.</p>
  
  <p>Here is the XAML:</p>
  
  <pre><code>&lt;data:DataGrid
        AutoGenerateColumns="False"
        IsReadOnly="True"
        ItemsSource="{Binding Path=Data}"
        SelectedItem="{Binding SelectedRow, Mode=TwoWay}"&gt;
     &lt;data:DataGrid.Columns&gt;
        &lt;data:DataGridTextColumn Binding="{Binding A}" /&gt;
        &lt;data:DataGridTextColumn Binding="{Binding B}" /&gt;
        &lt;data:DataGridTextColumn Binding="{Binding C}" /&gt;
     &lt;/data:DataGrid.Columns&gt;
     &lt;i:Interaction.Behaviors&gt;
        &lt;behaviors:EnterBehavior Command="{Binding CommitCommand}" /&gt;
     &lt;/i:Interaction.Behaviors&gt;
  &lt;/data:DataGrid&gt;
  </code></pre>
  
  <p>Can you tell me how I can prevent the default ENTER event?</p>