C# 这个XAML异常来自哪里?

C# 这个XAML异常来自哪里?,c#,xaml,C#,Xaml,我使用绑定组检查输入参数。突然,我在XAML中抛出了一个异常。好的,我们来看一些代码,我认为可能是相关的。首先是一块XAML: <Grid x:Name="TopGrid" DockPanel.Dock="Top" Height="269"> <Grid.BindingGroup> <BindingGroup Name="LengthBindingGrp"

我使用绑定组检查输入参数。突然,我在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>
这个错误似乎起源于线路

CableViewModel cvm = ....
因为我在那里演演员


但不知何故,这在以前起了作用。我是如何在XAML中获得此异常的?

是否可能有两个类(在不同的名称空间中)名为
CableViewModel
?您是否使用了像Resharper这样的扩展来自动修改名称空间?@Odrai:没有。我一周前用过它,但后来去掉了它。@Jeroen van Langen:确实把CableViewModel作为数据上下文弄乱了。我在XAML和代码隐藏中将其设置为DC。那不会飞。MVVM上的PluralSight视频也很有帮助。
    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 = ....