84

It is very easy to bind Buttons in WPF apps to Commands in a VIEWMODEL class. I'd like to achieve a similar binding for a TextBox.

I have a TextBox and I need to bind it to a Command that fires up when I hit Enter while the TextBox is focused. Currently, I'm using the following handler for the KeyUp event, but it looks ugly... and I can't put it in my VIEWMODEL class.

private void TextBox_KeyUp(object sender, KeyEventArgs e)
{
    if (e.Key == System.Windows.Input.Key.Enter)
    {
        // your event handler here
        e.Handled = true;
        MessageBox.Show("Enter Key is pressed!");
    }
}

Is there a better way to do this?

5 Answers 5

256

I've faced with the same problem and found solution here, here is the code sample:

<TextBox>
  <TextBox.InputBindings>
    <KeyBinding Command="{Binding Path=CmdSomething}" Key="Enter" />
  </TextBox.InputBindings>
</TextBox>
7
  • 24
    NB you may need to set UpdateSourceTrigger=PropertyChanged on your TextBox binding for this to work. Aug 5, 2013 at 21:09
  • 1
    that's stupid easy. except it doesn't work for AutoCompleteBox from the WPF Toolkit - for that see this stackoverflow.com/questions/4996731 Mar 6, 2014 at 0:09
  • 4
    Richard Dingwall tell the terrible truth. what we need is 2 things: (1) Button.IsDefault="True" (2) TextBox Text Binding add "UpdateSourceTrigger=PropertyChanged ". nothing more. even sarh's answer not to be used.
    – Haiyuan Li
    Sep 29, 2014 at 5:31
  • 1
    @HaiyuanLi: Button.IsDefault doesn't help much, if each textbox is to trigger a different button and/or bound command.
    – Ben Voigt
    Mar 1, 2016 at 23:54
  • This doesn't work at all. 'UpdateSourceTrigger' is set to 'PropertyChanged'. I use the same command for a button in the same grid but the textbox doesn't trigger it. Aug 12, 2016 at 7:06
23

Aryan, not every WPF object supports commanding. So if you wan't to do that you'll need either to call your view model from your code behind (a little coupled) or use some MVVM Messaging implementation to decouple that. See MVVM Light Messaging toolkit for an example. Or simple use triggers like this:

<TextBox>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="KeyUp">
            <i:InvokeDataCommand Command="{Binding MyCommand}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</TextBox>
5
  • Ok thank you so much for replying....but this command will fires up on each key up event or just a ENTER key up event?? Jul 30, 2011 at 9:25
  • Every time. If you wan't just for the enter then you'll have to write a markup extension to do that, like this one: tomlev2.wordpress.com/2009/03/17/…
    – Erre Efe
    Jul 30, 2011 at 9:31
  • 3
    See this is the nice example i have found over here Jul 30, 2011 at 9:50
  • 2
    Thanks. Current event trigger is <i:InvokeCommandAction Command="{Binding QuickSearchTextKeyUp}"/>
    – Nexxas
    Dec 18, 2013 at 14:42
  • how to apply this trigger on multiple textboxes
    – Meer
    Feb 12, 2016 at 5:23
19

I like Sarh's answer, but it wouldn't work in my program, unless I changed Enter to Return:

<TextBox>
    <TextBox.InputBindings>
        <KeyBinding Key="Return" Command="{}" />
   </TextBox.InputBindings>
</TextBox>
1
  • I made the same experience May 3, 2020 at 8:15
7
<TextBox Text="{Binding FieldThatIAmBindingToo, UpdateSourceTrigger=PropertyChanged}">
    <TextBox.InputBindings>
        <KeyBinding Command="{Binding AddCommand}" Key="Return" />
    </TextBox.InputBindings>
</TextBox>

I took answer from here

-9

You can set true to AcceptReturn property.

 <TextBox AcceptsReturn="True" />
1
  • 12
    All this does is allow the textbox to be multiline and handle the keypress itself, rather than triggering the default button action. Feb 12, 2015 at 9:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.