Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6548367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:57:51+00:00 2026-05-25T11:57:51+00:00

My setting is as described below: WPF 4 application MVVM Lite framework An Add

  • 0

My setting is as described below:

  • WPF 4 application
  • MVVM Lite framework
  • An “Add Item” window is shown on top of the main window in dialog mode (using view.ShowDialog();)
  • The dialog has a number of input fields, and the “Save” button that has it IsDefault property set to True
  • “Save” button is handled using a binding to a Command
  • Data binding is defined between the input fields in view’s XAML and the corresponding properties in the ViewModel (one way, for the UI to update the ViewModel, with Mode=OneWayToSource)

The problem is that when I press Enter on the keyboard, the value that I provided in the last input field is not pushed to the underlying property of the ViewModel.

I suspect that this has something to do with the fact that the input field hasn’t lost the focus before the window is closed (and thus all the bindings are “dissolved”).
For comparison, if I click the “Save” button (instead of letting its Click being handled by the window on Enter), the value is updated in the property.
Moreover, if I add (horror! horror!) an event handler to the button’s Click event, and call button.Focus() in code-behind, everything works!

What can be the remedy?

I obviously don’t want to handle any window closing events, and “manually” fetch the missing values… This would violate the whole MVVM concept 🙁

Any better suggestions?

Thank you,
Alex

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:57:52+00:00Added an answer on May 25, 2026 at 11:57 am

    By default, a TextBox only notifies it’s source that its value has changed once it loses focus. You can change that in your binding by setting UpdateSourceTrigger=PropertyChanged. This will make the TextBox send an update notification to its source its Text gets changed instead of only when it loses focus.

    If you don’t want to send an update notification anytime a key is pressed, you can create an AttachedProperty to update the source if the Enter key is pressed.

    Here’s the AttachedProperty that I use for situations like this:

    // When set to True, Enter Key will update Source
    #region EnterUpdatesTextSource DependencyProperty
    
    // Property to determine if the Enter key should update the source. Default is False
    public static readonly DependencyProperty EnterUpdatesTextSourceProperty =
        DependencyProperty.RegisterAttached("EnterUpdatesTextSource", typeof (bool),
                                            typeof (TextBoxHelper),
                                            new PropertyMetadata(false, EnterUpdatesTextSourcePropertyChanged));
    
    // Get
    public static bool GetEnterUpdatesTextSource(DependencyObject obj)
    {
        return (bool) obj.GetValue(EnterUpdatesTextSourceProperty);
    }
    
    // Set
    public static void SetEnterUpdatesTextSource(DependencyObject obj, bool value)
    {
        obj.SetValue(EnterUpdatesTextSourceProperty, value);
    }
    
    // Changed Event - Attach PreviewKeyDown handler
    private static void EnterUpdatesTextSourcePropertyChanged(DependencyObject obj,
                                                              DependencyPropertyChangedEventArgs e)
    {
        var sender = obj as UIElement;
        if (obj != null)
        {
            if ((bool) e.NewValue)
            {
                sender.PreviewKeyDown += OnPreviewKeyDownUpdateSourceIfEnter;
            }
            else
            {
                sender.PreviewKeyDown -= OnPreviewKeyDownUpdateSourceIfEnter;
            }
        }
    }
    
    // If key being pressed is the Enter key, and EnterUpdatesTextSource is set to true, then update source for Text property
    private static void OnPreviewKeyDownUpdateSourceIfEnter(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            if (GetEnterUpdatesTextSource((DependencyObject) sender))
            {
                var obj = sender as UIElement;
                BindingExpression textBinding = BindingOperations.GetBindingExpression(
                    obj, TextBox.TextProperty);
    
                if (textBinding != null)
                    textBinding.UpdateSource();
            }
        }
    }
    
    #endregion //EnterUpdatesTextSource DependencyProperty
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing a WPF application using the MVVM pattern as described by Josh
Any ideas? I've tried setting webdriver.firefox.useExisting=true as described in the link below hoping to
Setting up ASP.net MVC with Linq2SQL or Entity Framework's context to have scaffolding work
Setting up a Windows based web application on Amazon's cloud has definitely been a
Setting onchange event for CheckBoxList using the following code doesn't work. chkListUserGroup.Attributes.Add(onchange, document.forms[0].isRecordModified.value='true';); How
Setting the scene: My asp.net web application carries a version number which is incremented
The below text is an effort to expand and add color to this question:
Hi I am using Resoursce files as described here: http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.aspx I am setting the
I have configured WordPress to display a static front page as described here: http://codex.wordpress.org/Settings_Reading_SubPanel#Reading_Settings
Setting up an integration server, I’m in doubt about the best approach regarding using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.