50 lines
1.6 KiB
C#
50 lines
1.6 KiB
C#
using System.Globalization;
|
|
using System.Reflection;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Data;
|
|
|
|
using BS.Shared.Extensions;
|
|
|
|
namespace BeWo.Validation
|
|
{
|
|
public class AttributeValidator : ValidationRule
|
|
{
|
|
public BindingExpression ExpressionToValidate { get; set; }
|
|
|
|
public override ValidationResult Validate(object pValue, CultureInfo pCultureInfo)
|
|
{
|
|
bool lValid = true;
|
|
|
|
if (this.ExpressionToValidate != null)
|
|
{
|
|
PropertyInfo lPropInfo = null;
|
|
object lCurrentObject = this.ExpressionToValidate.DataItem;
|
|
if (lCurrentObject != null)
|
|
{
|
|
foreach (var iPropName in this.ExpressionToValidate.ParentBinding.Path.Path.Split("."))
|
|
{
|
|
lPropInfo = lCurrentObject.GetType().GetProperty(iPropName);
|
|
lCurrentObject = lPropInfo.GetValue(lCurrentObject, null);
|
|
}
|
|
}
|
|
|
|
if (lPropInfo != null)
|
|
{
|
|
var lAttributes = lPropInfo.GetCustomAttributes(typeof(ValidationAttribute), true);
|
|
if (lAttributes.Length > 0)
|
|
{
|
|
lValid = ((ValidationAttribute) lAttributes[0]).Validate(pValue);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!lValid)
|
|
{
|
|
|
|
}
|
|
|
|
// TODO: Nachricht, am besten als Property im ValidationAttribut
|
|
return new ValidationResult(lValid, null);
|
|
}
|
|
}
|
|
} |