|
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
namespace ValidationRuleWithBoundProperty
{
/// <summary>
/// Ensures that the validated value is a multiple of a specified number.
/// </summary>
public class IsMultipleOfValidationRule : ValidationRule
{
private IntegerContainer divisorContainer;
public IntegerContainer DivisorContainer
{
get { return divisorContainer; }
set { divisorContainer = value; }
}
public override ValidationResult Validate( object value, CultureInfo cultureInfo )
{
try
{
int dividend = (int)Convert.ChangeType( value, typeof( int ) );
int divisor = this.DivisorContainer.Value;
if( dividend % divisor == 0 )
return ValidationResult.ValidResult;
return new ValidationResult(
false,
"The number is not a multiple of " + divisor );
}
catch
{
return new ValidationResult( false, "Not a number." );
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.