none
Force a field to be updated RRS feed

  • Question

  • Hi,

    We have the following problem. All our controls activate their binding at the moment the field is left. In our Application saving of data must be transparant to the end-user, meaning, there's no save button. We save our data when the user switches masks (via the TabControl, the DevExpress NavBar control, etc... in the SelectionChanged event, and when the Application is shut down).

    When a user starts typing in a field and the Tab is switched or the Application is shut down, the field is in fact never left and the entries in the field are lost. At the moment we change the focus using the Focusmanager but that has some nasty other side-effects.

    The Question. Can we some how activate the binding on a control without leaving it?

    Regards
    Paul Sinnema


    Take a look at my blog: http://paul.sinnema.ch
    Friday, March 19, 2010 9:43 AM

Answers

  • Hi Jie,

    We've solved it as follows belows. We created an Interface which we can call from our Controls for validation (Validation is an issue we haven't addressed yet but the binding UpdateSource is needed anyway).

    Regards
    Paul Sinnema
    Diartis AG

    In our IO Controls (like TextBox, DatePicker, ComboBox, etc:

            public void Validate()
            {
                // The source is updated via the binding
                FrameworkElements.UpdateSource(this, KLIBDatePicker.TextProperty);
            }
    
    

    In a separate static class called FrameworkElements:

            /// <summary>
            /// Update the source via the BindingExpression of the control.
            /// </summary>
            /// <param name="frameworkElement"></param>
            /// <param name="dependencyProperty"></param>
            public static void UpdateSource(FrameworkElement frameworkElement, DependencyProperty dependencyProperty)
            {
                Assertions.CheckObject("frameworkElement", frameworkElement);
                Assertions.CheckObject("dependencyProperty", dependencyProperty);
    
                BindingExpression bindingExpression = frameworkElement.GetBindingExpression(dependencyProperty);
    
                if (bindingExpression != null)
                {
                    bindingExpression.UpdateSource();
                }
            }

    Take a look at my blog: http://paul.sinnema.ch

    • Marked as answer by PaulSinnema Wednesday, March 24, 2010 6:13 PM
    Wednesday, March 24, 2010 6:12 PM

All replies

  • Hi,

    Thanks for the reply.

    No, setting the UpdateSourceTrigger to PropertyChanged is not what we want. It cause a round-trip to the ViewModel (and Model) on each character typed and would therefore cause a slowness that is not acceptable.

    I've read something about the UpdateSource() method on the BindingExpression. At the moment the SaveChanges() takes place, we do not have the DependencyProperty. The only thing we have is the FocusedElement on the Keyboard class. How can we call the UpdateSource() on the FocusedElement without knowing its DependenceProperty?

    Regards
    Paul Sinnema


    Take a look at my blog: http://paul.sinnema.ch
    Friday, March 19, 2010 12:06 PM
  • Hello Paul,

    Is it possible to organize your bindings in a BindingGroup so when necessary you can just call the UpdateSource on the BindingGroup? Thus all the BindingExpressions in that BindingGroup will be updated.

    And one thing I don't quite understand is, why all the controls activate their binding at the moment the field is left? Any specific reasons?

    Thanks,
    Jie
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    If you have any feedback, please tell us.

    The All-In-One Code Framework Project
    My Blog (in Simplified Chinese)
    Wednesday, March 24, 2010 11:52 AM
    Moderator
  • Hi Jie,

    We've solved it as follows belows. We created an Interface which we can call from our Controls for validation (Validation is an issue we haven't addressed yet but the binding UpdateSource is needed anyway).

    Regards
    Paul Sinnema
    Diartis AG

    In our IO Controls (like TextBox, DatePicker, ComboBox, etc:

            public void Validate()
            {
                // The source is updated via the binding
                FrameworkElements.UpdateSource(this, KLIBDatePicker.TextProperty);
            }
    
    

    In a separate static class called FrameworkElements:

            /// <summary>
            /// Update the source via the BindingExpression of the control.
            /// </summary>
            /// <param name="frameworkElement"></param>
            /// <param name="dependencyProperty"></param>
            public static void UpdateSource(FrameworkElement frameworkElement, DependencyProperty dependencyProperty)
            {
                Assertions.CheckObject("frameworkElement", frameworkElement);
                Assertions.CheckObject("dependencyProperty", dependencyProperty);
    
                BindingExpression bindingExpression = frameworkElement.GetBindingExpression(dependencyProperty);
    
                if (bindingExpression != null)
                {
                    bindingExpression.UpdateSource();
                }
            }

    Take a look at my blog: http://paul.sinnema.ch

    • Marked as answer by PaulSinnema Wednesday, March 24, 2010 6:13 PM
    Wednesday, March 24, 2010 6:12 PM
  • Jie,

    I just re-read my question. Now I understand what you are asking. The Binding is executed automatically by WPF upon changes in the value of the Control and leaving the field (UpdateSourceTrigger=LostFocus). The 'LostFocus' is exactly causing the problem. When we change masks (via f.i. TabControl) we do a SaveChanges() for all our Data. Because the TabControl doesn't take Focus away from the Field, the Binding on that Field is never triggered. This causes the Data not change in our ViewModel and Model and is therefore not stored in the DB.

    Regards
    Paul Sinnema
    Diartis AG


    Take a look at my blog: http://paul.sinnema.ch
    Wednesday, March 24, 2010 6:18 PM
  • This is an old thread but did anyone answer it?  I have the same problem.  if i have a field with (UpdateSourceTrigger=LostFocus), and I click on a button, the focus is not lost in the field, the validation is not triggered.  Is there a good way around this?

     

    Thanks

     

    Steve

    Tuesday, February 1, 2011 1:55 PM