Search code examples
wpfdata-bindingtextbox

mouse focus on a textbox not getting updated in wpf


I have two textboxes. The values are bind from properties:

<TextBlock Text="Input 1" Margin="3,3,3,3" FontWeight="Normal" Foreground="#FFF4E7CA"/>

<TextBox Text="{Binding Processing.Input1}" Margin="3,3,6,3" FontWeight="Normal"/>
    
<TextBlock Text="Input 2" Margin="3,3,3,3" FontWeight="Normal" Foreground="#FFF4E7CA"/>

<TextBox Text="{Binding Processing.Input2}" Margin="3,3,6,3" FontWeight="Normal"/>

After changing the values in the first textbox, I press on "Save" button. the value of the Textbox1 will be not updated on the saved result. Only if I move the mouse focus from textbox 1 after editing to Textbox 2. then the result got updated. How can I get the textbox updated to the property immediately?


Solution

  • TextBox by default updates the binding source on LostFocus. It seems that TextBox does not lose the focus by pressing the "Save" button.

    You can update the binding source immediately after every change with:

    Text="{Binding Processing.Input1, UpdateSourceTrigger=PropertyChanged}"
    

    or update the binding source explicit on "Save" button click:

    // textBox1 is your Textbox1
    BindingExpression be = textbox1.GetBindingExpression(TextBox.TextProperty);
    be.UpdateSource();