views:

148

answers:

1

Hi !

I have a control with this validation

<MyPicker.SelectedItem>
 <Binding Path="Person.Value" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" NotifyOnValidationError="True">
  <Binding.ValidationRules>
   <rules:MyValidationRule ValidationType="notnull"/>
  </Binding.ValidationRules>
 </Binding>
</MyPicker.SelectedItem>

This is the Validation Class:

class MyValidationRule : ValidationRule
{        
 private string _validationType;
 public string ValidationType
 {
  get { return _validationType; }
  set { _validationType = value;  }
 }

 public override ValidationResult Validate(object value, CultureInfo cultureInfo)
 {            
  ValidationResult trueResult = new ValidationResult(true, null);

  switch (_validationType.ToLower())
  {
   case "notnull": return value == null ? new ValidationResult(false, "EMPTY FIELD") : trueResult;               
   default: return trueResult;
  }
 }
}

Question: When the property is changed, then the Validate( ) method is called which is correct.

But to call this method at the very beginning when the MyControl is created? I need to prove immediate after initialize if the there's a null value in the control (and display a validation error)

A: 

OK I've solved it: You force the validation when the element got bound with a simple property - ValidatesOnTargetUpdated:

 <rules:MyValidationRule ValidatesOnTargetUpdated="True"  ValidationType="notnull"/>
PaN1C_Showt1Me