0
votes

I have a WPF application. I am using MVVM. I have Person Model which is bound to WPF CreatePerson view. In CreatePerson view, There is textbox in which I bound Text property to Person.FirstName Now my default focus to FirstName textbox. When I hit tab key without doing anything, it does not fire validation, but when i write something then it fires properly.

All validations are set properly.

Now I want when I hit tab key, it should fire validation.

If more clearance is required, please let me know.

1
Since you aren't changing the property, validation isn't firing, I think you would have to attach a behaviour to force validation - it sounds like a 'by design' issue - Charleh
code is very much simple. Think a scenario. You want to create a person, so you launch that screen. Focus is set to first input value. Now you hit tab key, there is no validation fires. we can achieve this in web application easily. - rajnikant rajwadi
We can't think of a scenario without you showing us what you tried. Give us the code so we can see what you did wrong and explain it to you! - Damascus
also there is a way to fire validation on lostfocus in binding, is there any way, when it got focuses. - rajnikant rajwadi

1 Answers

1
votes

the validation is called when any attempt to update the binding source occurs. If you want to fire validation when the events GotFocus occurs, you must update your binding programmatically, i.e. in the event handler of GotFocus you can force the binding

private void textBox1_GotFocus(object sender, RoutedEventArgs e)
{
      BindingExpression binding = BindingOperations.GetBindingExpression(textBox1, TextBox.TextProperty);
 binding.UpdateSource(); 
}