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

Posted on 2012-08-13
Medium Priority
Last Modified: 2012-08-14
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.
Question by:hpdvs2
1 Comment

Accepted Solution

mvcamp earned 2000 total points
ID: 38291110

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.


Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month14 days, 2 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question