EndEdit equivalent in WPF
To avoid the issue of needing to tab away, you could simply change the UpdateSourceTrigger property of your controls' binding. Try the following:
<TextBox.Text>
<Binding Path="MyProperty" UpdateSourceTrigger="PropertyChanged"/>
</TextBox.Text>
This tells WPF to update the backing object whenever the Text property is changed. This way, you don't need to worry about tabbing away. Hope this helps!
EDIT:
The accepted answer for the following SO question provides a way to automatically run validation rules for a page. You could modify it to call UpdateSource() on all BindingExpression objects instead.
Link
You can force specific bindings to update using code like the following:
var bindingExpression = txtInput.GetBindingExpression(TextBox.TextProperty);
bindingExpression.UpdateSource();
Doing this more generally is difficult because there is no generic way to get all bindings, nor would you necessarily want them all to updated.
Based on Pwninstein answer, I have now implemented an EndEdit
in my common class for WPF Views / Windows that will look for bindings and force an update on them, code below;
Code below;
private void EndEdit(DependencyObject parent)
{
LocalValueEnumerator localValues = parent.GetLocalValueEnumerator();
while (localValues.MoveNext())
{
LocalValueEntry entry = localValues.Current;
if (BindingOperations.IsDataBound(parent, entry.Property))
{
BindingExpression binding = BindingOperations.GetBindingExpression(parent, entry.Property);
if (binding != null)
{
binding.UpdateSource();
}
}
}
for(int i=0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(parent, i);
this.EndEdit(child);
}
}
protected void EndEdit()
{
this.EndEdit(this);
}
In my Save command, I now just call the EndEdit
method, and I don't have to worry about other programmers selection of binding method.