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

Posted on 2012-08-13
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
    LVL 1

    Accepted Solution


    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

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    729 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now