ValidationRule.ValidatesOnTargetUpdated Property

Definition

Gets or sets a value that indicates whether the validation rule runs when the target of the Binding is updated.

C#
public bool ValidatesOnTargetUpdated { get; set; }

Property Value

true if the validation rule runs when the target of the Binding is updated; otherwise, false.

Examples

The following example checks whether the TextBox is empty. The ValidationRule, ValueIsNotNull, has ValidatesOnTargetUpdated set to true, so that when the application starts, the ValidationRule runs and displays a message if the TextBox is empty.

XAML
<TextBox Width="150"
         Validation.Error="ItemError">
  <TextBox.Text>
    <Binding Source="{StaticResource myObject}"
             Path="PropertyB"
             UpdateSourceTrigger="PropertyChanged"
             NotifyOnValidationError="True">
      <Binding.ValidationRules>
        <src:ValueIsNotNull ValidatesOnTargetUpdated="True" />
      </Binding.ValidationRules>
    </Binding>
  </TextBox.Text>
</TextBox>

The following example shows the ValidationRule that is used in the previous example and the event handler for the Error event.

C#
public class ValueIsNotNull : ValidationRule
{
    public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
    {
        string str = value as string;

        if (!string.IsNullOrEmpty(str))
        {
            return ValidationResult.ValidResult;
        }
        else
        {
            return new ValidationResult(false, "Value must not be null");
        }
    }
}

Applies to

Product Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10