to make sure one property change trigger the other property run validation logic,
must call OnVilidate Code into IDataErrorInfo type of logic in its property Setter
<TextBox Grid.Column="1" Text="{Binding ...,ValidatesOnDataErrors=True,NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource TextBoxWithValidationStyle}">
[MustBeGreaterLessThanAllowSuffix("TheOther", false, true, ErrorMessage = "Please specify ..")]
using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = true)]
public class MustBeGreaterLessThanAllowSuffixAttribute : ValidationAttribute
{
public string CompareToPropertyName { get; private set; }
public bool Greater { get; private set; }
public bool IncludeEqual { get; private set; }
public object[] Values { get; private set; }
private static readonly char[] SUFFIXES = new char[4] { 'M', 'm', 'K', 'k' };
public MustBeGreaterLessThanAllowSuffixAttribute(string compareToPropertyName,bool greater,bool includeEqual, params object[] values)
{
CompareToPropertyName = compareToPropertyName;
Greater = greater;
IncludeEqual = includeEqual;
Values = values;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var compToProperty = validationContext.ObjectType.GetProperty(CompareToPropertyName);
var compToValue = compToProperty.GetValue(validationContext.ObjectInstance, null);
return VerifyValidGreaterThanCondition(value, compToValue) ? ValidationResult.Success : new ValidationResult(ErrorMessage);
}
private bool VerifyValidGreaterThanCondition(object value, object compToValue)
{
if (value == null || compToValue == null)
{
return true;
}
string valueNoSuffix = value.ToString().TrimEnd(SUFFIXES);
string compToValueNoSuffix = compToValue.ToString().TrimEnd(SUFFIXES);
double dValue, dCompToValue;
if (!double.TryParse(valueNoSuffix, out dValue) || !double.TryParse(compToValueNoSuffix, out dCompToValue))
{
return true;
}
var dValueMutiplied = dValue * GetMultiplier(value);
var dCompToValueMutiplied = dCompToValue * GetMultiplier(compToValue);
if (Greater)
{
return IncludeEqual ? dValueMutiplied >= dCompToValueMutiplied : dValueMutiplied > dCompToValueMutiplied;
}
return IncludeEqual ? dValueMutiplied <= dCompToValueMutiplied : dValueMutiplied < dCompToValueMutiplied;
}
private double GetMultiplier(object value)
{
if(value==null)
return 1.0;
char suffix = value.ToString().LastOrDefault();
switch (suffix)
{
case 'M':
case 'm':
return 1E6;
case 'K':
case 'k':
return 1E3;
}
return 1.0;
}
[CustomValidation(typeof(VegaTraderSettingValidator), "Check")]
using System.ComponentModel.DataAnnotations;
public class VegaTraderSettingValidator
{
public static ValidationResult Check(string s)
{
return ValidationResult.Success;
}
}
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = true)]
public class MutualExclusiveDataEntryAttribute : ValidationAttribute
{
public string MutualExclusivePropertyName { get; private set; }
public MutualExclusiveDataEntryAttribute(string mutualExclusivePropertyName)
{
MutualExclusivePropertyName = mutualExclusivePropertyName;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var mutProperty = validationContext.ObjectType.GetProperty(MutualExclusivePropertyName);
if (mutProperty == null)
return ValidationResult.Success;
var mutValue = mutProperty.GetValue(validationContext.ObjectInstance, null);
return VerifyMutualExclsivity(value, mutValue) ? ValidationResult.Success : new ValidationResult(ErrorMessage);
}
private bool VerifyMutualExclsivity(object value, object mutValue)
{
if (value == null || mutValue == null)
return true;
if (!value.IsDefault() && !mutValue.IsDefault())
return false;
return true;
}
}
Tuesday, November 22, 2016
Validate Two properties
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment