XAML Text Box MVVM Binding doesn't update with a default button...

I'm failry new to XAML.  I have this text box and button.

<TextBox Text="{Binding IdText}" Name="textBox1" Height="24" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="152" />
<Button IsDefault="True" Command="{Binding LookupIdCommand}" Name="button1"  Content="Lookup" Height="23" HorizontalAlignment="Left" Margin="168,11,0,0"VerticalAlignment="Top" Width="75" />

Open in new window


If I press enter in the text box, it does click the button for me, but the ViewModel does *NOT* get updated from the IdText.  If I type in the id, then click the button with the mouse, the viewModel does get updated prior with the text from IdText.

This is for a school, where students sign in with the keyboard.  Just a numeric key pad, no mouse no full keyboard (no tab key)  I need kids to be able to press enter and have it accept the text.
LVL 8
hpdvs2Asked:
Who is Participating?
 
mvcampConnect With a Mentor Commented:
Hi,

I'm using mvvmlight model from GalaSoft for our projects.
The fact that your IdText is not updated when pressing the enter key is because of there is no focuschanged from the textbox towards the button (or something else).
Hence another mechanism for updating your property is needed.
You can accomplish that by adding in the XAML an UpdateSourceTrigger.  Each keystroke in the textbox will fire this trigger (in the viewmodel the handler can be used for e.g. input validation).

<TextBox Text="{Binding IdText, UpdateSourceTrigger=PropertyChanged}" Name="textBox1" Height="24" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="152" />

Open in new window


Attached I've included a test project demonstrating this.

Hope this helps.

Regards
Marc
ButtonClick.zip
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.