79326549

Date: 2025-01-03 13:11:55
Score: 1
Natty:
Report link

When TextBox lost it's focus, then Binding is going to update data source. So check the source before the update, and you can get the previous value.

Example:

void TextBox_OnLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs args) {
    var textbox = sender as TextBox;
    var bx = textbox.GetBindingExpression(TextBox.TextProperty);
    var item = bx.ResolvedSource as MyDataItem; // source object
    var path = bx.ResolvedSourcePropertyName; // source property
    var previousValue = item.MyProperty;
    bool dirty = bx.IsDirty; // if true, update fires
}

Note: If user input the same value as the previous, it will also be marked as "dirty".

https://learn.microsoft.com/en-us/dotnet/desktop/wpf/data/how-to-control-when-the-textbox-text-updates-the-source?view=netframeworkdesktop-4.8

Reasons:
  • Probably link only (1):
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Starts with a question (0.5): When
  • Low reputation (0.5):
Posted by: Masaki Ohashi