Group 2: TextBox Validation

The second group box consists only of a TextBox. Type some text into the TextBox to start the validation process. TX Spell .NET is used to validate the text by checking the spelling.

The result of this sample is very similar to the first group, but it uses a ValidationRule that uses TX Spell .NET to validate the spelling. It returns false, if the spelling is not correct. The text box is validated everytime the TextBox content is changed.

[C#]
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
	TXSpell.Check((string)value);

	if (TXSpell.IncorrectWords.Count > 0)
	{
		return new ValidationResult(false, "Misspelled.");
	}

	return new ValidationResult(true, null);
}
[Visual Basic]
Public Overrides Function Validate(ByVal value As Object, _
	ByVal cultureInfo As Globalization.CultureInfo) As ValidationResult
	TXSpell.Check(DirectCast(value, String))

	If TXSpell.IncorrectWords.Count > 0 Then
		Return New ValidationResult(False, "Misspelled.")
	End If

	Return New ValidationResult(True, Nothing)
End Function

The validation rule is added to the TextBox using a binding path in the XAML view:

<TextBox x:Name="Box" Validation.ErrorTemplate="{StaticResource TextBoxErrorTemplate}" Margin="9,40,12,58" Height="23">
	<TextBox.Text>
		<Binding Path="ValueInBox" UpdateSourceTrigger="PropertyChanged">
			<Binding.ValidationRules>
				<CheckSimpleText:SpellingRule x:Name="spellingRule1"></CheckSimpleText:SpellingRule>
			</Binding.ValidationRules>
		</Binding>
	</TextBox.Text>
</TextBox>