WPF ValidationRule Validate when the control is loaded

Posted by PaN1C_Showt1Me on Stack Overflow See other posts from Stack Overflow or by PaN1C_Showt1Me
Published on 2010-04-15T09:59:20Z Indexed on 2010/04/15 13:23 UTC
Read the original article Hit count: 402

Filed under:
|
|

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)

© Stack Overflow or respective owner

Related posts about wpf

Related posts about validation