0

I have following WPF-MVVM setup:

  • A view with 2 textfields bound to the properties of the ViewModel with UpdateSourceTrigger=PropertyChanged and a button with a command binding to a relay command (CanExecute, Execute methods)
  • ViewModel which implements INotifyDataErrorInfo with 2 properties annotated with [System.ComponentModel.DataAnnotations.Required]. Property setter call private ViladateProperty method, which stores the validation results in the errors dictionary. HasErrors property getter checks the errors dictionary to return the appropriate value.

The aim is to enable the button as soon as the whole form validates correctly. My two questions on this are:

  • How to implement the CanExecute method of the button relay command without calling the validation on the whole model for every property change?
  • What is the best way to "delay" the UpdateSourceTrigger to set the according property not on every keystroke but, for example, after one second of "no input"?

1 Answer 1

1

Your CanExecute handler should simply refer to your HasErrors property:

... (canExecute) => !HasErrors; ...

•How to implement the CanExecute method of the button relay command without calling the validation on the whole model for every property change?

Can you tell me how the property system could possibly know if there were any validation errors if it didn't check after every key stroke? Think about it... any key stroke could make the model invalid. Either way, you won't notice any delay as it revalidates the model.

•What is the best way to "delay" the UpdateSourceTrigger to set the according property not on every keystroke but, for example, after one second of "no input"?

If you're using .NET 4.5, you're in luck... Microsoft just added a Delay property to the Binding class. This enables you to set the amount of time, in milliseconds, to wait before updating the binding source after the value on the target changes. For full information, please refer to the BindingBase.Delay Property page on MSDN.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the answer to the second question! About the first question - my CanExecute handler only references HasErrors as you described. The point is the validation strategy: If you validate all properties on every property change the second textfiled (without input) becomes invalid as soon as the first one becomes empty and vice versa. If you validate only the currently changed property you can get an invalid form valid because other textfield hasn't been validated. The question is how to get all properties validated without validating these on every property change.
I'm not sure how you've wired up your validation, or even whether you're talking about business rule validation or just data type validation, but I don't manipulate the UI controls at all when the data becomes invalid. I just make the whole form glow red and display the exact error messages in a collection in the UI for the user to fix.
It' s more generic question: when to invoke the whole form validation (read: validation on every property) if you validating one property at a changing event of this property?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.