Tutorial :WPF lengthy Validation Scenario


  • in my scenario I have a Linq2SQL Data backend.
  • my Dataobjects implement IDataErrorInfo to catch errors like Name==null (fast to execute Validationrules that only require the value, nothing special so far )

  • the Dataobjects are organized in a tree-structure, so each has a Parent and Children

How can I validate if a chosen Name is Unique under the Children of a Dataobjects' Parent?

The problem I'm facing is, that the unique Name validation requires a Database roundtrip which lags typing if UpdateSourceTrigger="PropertyChanged" on the TextBox binding to the Name.

On the other hand, I could set UpdateSourceTrigger="LostFocus", but the problem with that is, that I enable/disable a "Save" button on valid/invalid data. Now in the invalid state you can't click the Save button, so there's no way the Textbox could lose Focus to update (only tabbing away which is ugly, but there are more "unusabilities" with LostFocus (e.g. Error keeps displaying while typing and thus changing the name).

what would be ideal was a way to say for individual validationrules to apply on different events like so:

<TextBox Grid.Column="1">      <TextBox.Text>          <Binding Path="Foldername">              <Binding.ValidationRules>                  <wpfresources:UniqueChildValidationRule ValidationStep="UpdatedValue" **UpdateSourceTrigger="LostFocus"**>                      ... stuff here ...                  </wpfresources:UniqueChildValidationRule>                  <DataErrorValidationRule **UpdateSourceTrigger="PropertyChanged"**/>              </Binding.ValidationRules>          </Binding>      </TextBox.Text>  </TextBox>  

What is the best way to solve this?


This MSDN article seems to suggest, that a BindingGroup would be the way to go. I'll look into that...


I finally figured a way to accomplish what I wanted.

The use of BindingGroup worked, but I stripped my solution down because what I needed essentially was a way to show a Validationerror in the correct Textbox.

I did so now with an attached property that I read out on execution of the "save"-functionality. then I manually validate and set a ValidationError on the textbox. that way the textbox can keep its PropertyChanged UpdateSourceTrigger and the lengthy validation is done on submit.


For what it's worth, validation that requires potentially arbitrary time (talking to a database over a network, e.g.) is one form of validation that does not need to be reflected in the UI in realtime. If you can work it so that it is (pre-caching the values that will be checked by reading them from the DB before you need them, e.g.), so much the better, but this is the one scenario where reporting an error after the user has submitted the data is generally acceptable as long as you don't destroy the information that the user entered.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »