我使用绑定组来检查输入参数。突然间,我在XAML中抛出异常。好的,我们在这里使用一些我认为可能相关的代码。首先是XAML的一部分:
<Grid
x:Name="TopGrid"
DockPanel.Dock="Top"
Height="269">
<Grid.BindingGroup>
<BindingGroup
Name="LengthBindingGrp"
NotifyOnValidationError="True">
<BindingGroup.ValidationRules>
<c:LengthValidationRule
ValidationStep="ConvertedProposedValue" />
</BindingGroup.ValidationRules>
</BindingGroup>
</Grid.BindingGroup>
<Grid.RowDefinitions>
这是验证程序:
public class LengthValidationRule : ValidationRule
{
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
BindingGroup bindingGroup = (BindingGroup)value;
CableViewModel cvm = ((CableViewModel)bindingGroup.Items[0]);
double x = cvm.RHSupportX;
if (x < 0)
return new ValidationResult(false, Resources.RHSupport_Right);
if (cvm.RHSupportY < 0)
return new ValidationResult(false, Resources.RHSupportNotHigher);
ObservableCollection<LoadCombi> loads = cvm.Loads;
foreach (LoadCombi ld in loads)
{
if (ld.Position < 0 || (ld.Position > x && !ld.Fixed) || (ld.Fixed && ld.Position > cvm.Ucl))
return new ValidationResult(false, String.Format(Resources.LoadPosition, ld.Position ));
}
return ValidationResult.ValidResult;
}
}
然后我在XAML中看到了这个错误屏幕:
InvalidCastException: Unable to cast object of type '_.di1.CableCalc.CableViewModel' to type 'CableCalc.CableViewModel'.
Stack Trace
at CableCalc.LengthValidationRule.Validate(Object value, CultureInfo cultureInfo)
at System.Windows.Controls.ValidationRule.Validate(Object value, CultureInfo cultureInfo, BindingGroup owner)
at System.Windows.Data.BindingGroup.CheckValidationRules()
at System.Windows.Data.BindingGroup.UpdateAndValidate(ValidationStep validationStep)
at System.Windows.Controls.DataGridRow.DelayedValidateWithoutUpdate(Object arg)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
错误似乎起源于
行CableViewModel cvm = ....
因为我有一个演员。
但不知怎的,这在以前有效。我怎么做才能在XAML中获得此异常?