DataBinding错误

时间:2011-10-04 21:24:29

标签: c# wpf data-binding reflection datatemplate

我希望我的标题可以更具描述性,但我无法理解下面的例外情况。

我最近发生了这种情况,结果证明是未正确声明的DependencyProperty,但我发现没有这个例外的帮助。

我可以添加的唯一另一件事就是它在调用我的GetHashCode方法时发生,它触发了一个通过反映对象的公共属性来派生哈希的方法。在这种情况下,该对象是从ViewModelBase类派生的,异常会在它调用Item的属性上被触发,我猜测它是IDataErrorInfo的一部分索引器,但这只是猜测。

所有这些都说明了,我的直觉是,在输出任何有用的调试信息之前,它只是一个糟糕的DataBinding。

有什么建议吗?

干杯,
Berryl

System.Reflection.TargetParameterCountException was unhandled by user code
  Message=Parameter count mismatch.
  Source=mscorlib
  StackTrace:
       at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
       at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
       at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index)
       at Smack.Core.Lib.DomainSuperTypes.EntityImpl.BaseObject.GetHashCode() in C:\Users\Lord & Master\Documents\Projects\Smack\trunk\src\Core\DomainSuperTypes\EntityImpl\BaseObject.cs:line 51
       at Smack.Core.Lib.DomainSuperTypes.EntityImpl.ValueObject.GetHashCode() in C:\Users\Lord & Master\Documents\Projects\Smack\trunk\src\Core\DomainSuperTypes\EntityImpl\ValueObject.cs:line 68
       at System.Collections.Hashtable.get_Item(Object key)
       at System.ComponentModel.TypeDescriptor.NodeFor(Object instance, Boolean createDelegator)
       at System.ComponentModel.TypeDescriptor.GetDescriptor(Object component, Boolean noCustomTypeDesc)
       at System.ComponentModel.TypeDescriptor.GetPropertiesImpl(Object component, Attribute[] attributes, Boolean noCustomTypeDesc, Boolean noAttributes)
       at System.Windows.PropertyPath.ResolvePropertyName(String name, Object item, Type ownerType, Object context, Boolean throwOnError)
       at System.Windows.PropertyPath.ResolvePropertyName(Int32 level, Object item, Type ownerType, Object context)
       at MS.Internal.Data.PropertyPathWorker.GetInfo(Int32 k, Object item, SourceValueState& svs)
       at MS.Internal.Data.PropertyPathWorker.ReplaceItem(Int32 k, Object newO, Object parent)
       at MS.Internal.Data.PropertyPathWorker.UpdateSourceValueState(Int32 k, ICollectionView collectionView, Object newValue, Boolean isASubPropertyChange)
       at MS.Internal.Data.ClrBindingWorker.AttachDataItem()
       at System.Windows.Data.BindingExpression.Activate(Object item)
       at System.Windows.Data.BindingExpression.AttachToContext(AttachAttempt attempt)
       at System.Windows.Data.BindingExpression.AttachOverride(DependencyObject target, DependencyProperty dp)
       at System.Windows.Data.BindingExpressionBase.Attach(DependencyObject target, DependencyProperty dp)
       at System.Windows.StyleHelper.GetInstanceValue(UncommonField`1 dataField, DependencyObject container, FrameworkElement feChild, FrameworkContentElement fceChild, Int32 childIndex, DependencyProperty dp, Int32 i, EffectiveValueEntry& entry)
       at System.Windows.StyleHelper.GetChildValueHelper(UncommonField`1 dataField, ItemStructList`1& valueLookupList, DependencyProperty dp, DependencyObject container, FrameworkObject child, Int32 childIndex, Boolean styleLookup, EffectiveValueEntry& entry, ValueLookupType& sourceType, FrameworkElementFactory templateRoot)
       at System.Windows.StyleHelper.GetChildValue(UncommonField`1 dataField, DependencyObject container, Int32 childIndex, FrameworkObject child, DependencyProperty dp, FrugalStructList`1& childRecordFromChildIndex, EffectiveValueEntry& entry, ValueLookupType& sourceType, FrameworkElementFactory templateRoot)
       at System.Windows.StyleHelper.GetValueFromTemplatedParent(DependencyObject container, Int32 childIndex, FrameworkObject child, DependencyProperty dp, FrugalStructList`1& childRecordFromChildIndex, FrameworkElementFactory templateRoot, EffectiveValueEntry& entry)
       at System.Windows.StyleHelper.ApplyTemplatedParentValue(DependencyObject container, FrameworkObject child, Int32 childIndex, FrugalStructList`1& childRecordFromChildIndex, DependencyProperty dp, FrameworkElementFactory templateRoot)
       at System.Windows.StyleHelper.InvalidatePropertiesOnTemplateNode(DependencyObject container, FrameworkObject child, Int32 childIndex, FrugalStructList`1& childRecordFromChildIndex, Boolean isDetach, FrameworkElementFactory templateRoot)
       at System.Windows.FrameworkTemplate.InvalidatePropertiesOnTemplate(DependencyObject container, Object currentObject)
       at System.Windows.FrameworkTemplate.<>c__DisplayClass6.<LoadOptimizedTemplateContent>b__3(Object sender, XamlObjectEventArgs args)
       at System.Xaml.XamlObjectWriter.Logic_CreateAndAssignToParentStart(ObjectWriterContext ctx)
       at System.Xaml.XamlObjectWriter.WriteStartMember(XamlMember property)
       at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlReader templateReader, XamlObjectWriter currentWriter)
  InnerException: 

更新

下面的

是捕获异常的行:

var value = property.GetValue(this, null);

这是此方法的一部分

    public override int GetHashCode() {
        unchecked {
            IEnumerable<PropertyInfo> signatureProperties = GetSignatureProperties().ToArray();

            // It's possible for two objects to return the same hash code based on 
            // identically valued properties, even if they're of two different types, 
            // so we include the object's type in the hash calculation
            var hashCode = GetType().GetHashCode();

            foreach (var property in signatureProperties) {
                var value = property.GetValue(this, null);

                if (value != null)
                    hashCode = (hashCode * HASH_MULTIPLIER) ^ value.GetHashCode();
            }

            if (signatureProperties.Any())
                return hashCode;

            // If no properties were flagged as being part of the signature of the object,
            // then simply return the hashcode of the base object as the hashcode.
            return base.GetHashCode();
        }
    }

1 个答案:

答案 0 :(得分:1)

当您调用property.GetValue(时,第二个参数用于指定索引器属性的索引值。如果属性是索引器并且您传递null,那么您将获得异常。您需要确定哪些属性是索引器,并且具有要作为第二个参数传入的值。

要添加引用,请根据documentation(在“例外”部分下),在以下情况下引发TargetParameterCountException

  

索引中的参数数量与数量不匹配   indexed属性所需的参数。

这听起来像你的具体情况。

希望这有帮助!