How do I change TextBox.Text without losing the binding in WPF?

If your binding is destroyed by setting a new value (which is strange, for a two way binding the binding should stay intact), then use ((TextBox)sender).SetCurrentValue(TextBox.TextProperty, newValue) to leave the binding intact.


Don't change the Text property - change what you are binding to.


var box = sender as TextBox;
// Change your box text..

box.GetBindingExpression(TextBox.TextProperty).UpdateSource();

This should force your binding to update.

Tags:

C#

Wpf

Mvvm