82

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?

Palec
  • 12,743
  • 8
  • 69
  • 138
Aryan SuryaWansi
  • 2,691
  • 10
  • 31
  • 33

5 Answers5

252

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>
sarh
  • 6,371
  • 4
  • 25
  • 29
  • 21
    NB you may need to set `UpdateSourceTrigger=PropertyChanged` on your TextBox binding for this to work. – Richard Dingwall Aug 05 '13 at 21:09
  • that's stupid easy. except it doesn't work for AutoCompleteBox from the WPF Toolkit - for that see this http://stackoverflow.com/questions/4996731 – Simon_Weaver Mar 06 '14 at 00: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 '14 at 05: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 01 '16 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. – Matthis Kohli Aug 12 '16 at 07:06
  • Greate, just set Mode=TwoWay, pdateSourceTrigger=PropertyChanged – Usman Ali Jan 08 '18 at 13:14
  • this will work just fine without any of those additives unless you expect the contents of the box to change... then you need `UpdateSourceTrigger=PropertyChanged` – MistaGoustan May 16 '18 at 14:16
22

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>
Erre Efe
  • 15,387
  • 10
  • 45
  • 77
  • 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?? – Aryan SuryaWansi Jul 30 '11 at 09: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: http://tomlev2.wordpress.com/2009/03/17/wpf-using-inputbindings-with-the-mvvm-pattern/ – Erre Efe Jul 30 '11 at 09:31
  • 3
    See this is the nice example i have found over [here](http://www.codeproject.com/Tips/170895/InputBinding-for-WPF-and-Silverlight-with-MVVM) – Aryan SuryaWansi Jul 30 '11 at 09:50
  • 2
    Thanks. Current event trigger is – Nexxas Dec 18 '13 at 14:42
  • how to apply this trigger on multiple textboxes – Meer Feb 12 '16 at 05:23
18

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>
Palec
  • 12,743
  • 8
  • 69
  • 138
Nightmare Games
  • 2,205
  • 6
  • 28
  • 46
6
<TextBox Text="{Binding FieldThatIAmBindingToo, UpdateSourceTrigger=PropertyChanged}">
    <TextBox.InputBindings>
        <KeyBinding Command="{Binding AddCommand}" Key="Return" />
    </TextBox.InputBindings>
</TextBox>

I took answer from here

Vovchikan
  • 61
  • 1
  • 2
-9

You can set true to AcceptReturn property.

 <TextBox AcceptsReturn="True" />
Kanimozhi
  • 19
  • 1
  • 12
    All this does is allow the textbox to be multiline and handle the keypress itself, rather than triggering the default button action. – Rudi Visser Feb 12 '15 at 09:18