2016-06-27 01:45:38 +02:00
|
|
|
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;
|
2026-07-30 14:17:47 +02:00
|
|
|
|
|
|
|
|
// Claude: DataItem des Bindings und Eltern-Objekt des Ziel-Propertys festhalten,
|
|
|
|
|
// damit kontextabhaengige Regeln den Klientenbezug auswerten koennen.
|
|
|
|
|
// Bei "PrototypeVM.Notice" ist das DataItem die ServiceRecordListVM (kennt
|
|
|
|
|
// WithoutClient), bei "Notice" in den EditViews die ServiceRecordVM selbst.
|
|
|
|
|
object lDataItem = lCurrentObject;
|
|
|
|
|
object lOwner = lCurrentObject;
|
|
|
|
|
|
2018-11-22 14:05:45 +01:00
|
|
|
if (lCurrentObject != null)
|
2016-06-27 01:45:38 +02:00
|
|
|
{
|
2018-11-22 14:05:45 +01:00
|
|
|
foreach (var iPropName in this.ExpressionToValidate.ParentBinding.Path.Path.Split("."))
|
|
|
|
|
{
|
2026-07-30 14:17:47 +02:00
|
|
|
lOwner = lCurrentObject; // Claude: vor dem GetValue merken
|
2018-11-22 14:05:45 +01:00
|
|
|
lPropInfo = lCurrentObject.GetType().GetProperty(iPropName);
|
|
|
|
|
lCurrentObject = lPropInfo.GetValue(lCurrentObject, null);
|
|
|
|
|
}
|
2016-06-27 01:45:38 +02:00
|
|
|
}
|
|
|
|
|
|
2018-11-22 14:05:45 +01:00
|
|
|
if (lPropInfo != null)
|
2016-06-27 01:45:38 +02:00
|
|
|
{
|
2018-11-22 14:05:45 +01:00
|
|
|
var lAttributes = lPropInfo.GetCustomAttributes(typeof(ValidationAttribute), true);
|
|
|
|
|
if (lAttributes.Length > 0)
|
|
|
|
|
{
|
2026-07-30 14:17:47 +02:00
|
|
|
// Claude: DataItem hat Vorrang, weil dort der View-Zustand liegt
|
|
|
|
|
// (ServiceRecordListVM.WithoutClient); sonst greift das Eltern-Objekt.
|
|
|
|
|
var lContext = (lDataItem as ICustomerRelatedContext)
|
|
|
|
|
?? (lOwner as ICustomerRelatedContext);
|
|
|
|
|
|
|
|
|
|
lValid = ((ValidationAttribute) lAttributes[0]).Validate(pValue, lContext);
|
2018-11-22 14:05:45 +01:00
|
|
|
}
|
2016-06-27 01:45:38 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!lValid)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: Nachricht, am besten als Property im ValidationAttribut
|
|
|
|
|
return new ValidationResult(lValid, null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|