Mega Code Archive
Specify Validation Rules for a Binding
//File:Window.xaml.cs
using System.Globalization;
using System.Windows.Controls;
namespace WpfApplication1
{
public class PercentageRule : ValidationRule
{
public override ValidationResult Validate(object value,CultureInfo cultureInfo)
{
string stringValue = value as string;
if(!string.IsNullOrEmpty(stringValue))
{
double doubleValue;
if(double.TryParse(stringValue, out doubleValue))
{
if(doubleValue >= 0 && doubleValue <= 100)
{
return new ValidationResult(true, null);
}
}
}
return new ValidationResult(false, "Must be a number between 0 and 100");
}
}
}