WPF错误:“System.Windows.Markup.XamlParseException:'DataGridRow'TargetType与元素'GridRowContent'的类型不匹配。”

时间:2015-04-08 08:54:12

标签: c# .net wpf visual-studio-2013

这个问题/答案对属于"分享您的知识,Q& A-style",旨在将此错误与最可能的原因联系起来。

我希望将来可能对某些开发人员有用。

错误:

System.Windows.Markup.XamlParseException: 'DataGridRow' TargetType does not match type of element 'GridRowContent'. ---> System.InvalidOperationException: 'DataGridRow' TargetType does not match type of element 'GridRowContent'.
        at System.Windows.Style.CheckTargetType(Object element)
        at System.Windows.StyleHelper.UpdateStyleCache(FrameworkElement fe, FrameworkContentElement fce, Style oldStyle, Style newStyle, Style& styleCache)
        at System.Windows.FrameworkElement.OnStyleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
        at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
        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.HandleBeforeProperties(Object createdObject, DependencyObject& rootObject, DependencyObject container, FrameworkElement feContainer, INameScope nameScope)
        at System.Windows.FrameworkTemplate.<>c__DisplayClass6.<LoadOptimizedTemplateContent>b__3(Object sender, XamlObjectEventArgs args)

1 个答案:

答案 0 :(得分:0)

当我遇到此特定错误时,根本原因是网格中的不兼容样式。

例如,如果您在网格中看到此代码段:

<grid:GridControl.View>
     <grid:TableView x:Name="tableView"
         VerticalScrollbarVisibility="Auto"
         DetailHeaderContent="Notification Tasks"
         AutoWidth="True"
         NavigationStyle="Row"
         RowStyle="{StaticResource MyStyle}"
         ShowGroupPanel="False"
         FadeSelectionOnLostFocus="False"
         ShowIndicator="False"
         >
     </grid:TableView>
</grid:GridControl.View>

然后,错误与RowStyle相关的行有关。 RowStyle指向无法找到,或者此网格的类型错误。当WPF搜索可视树时,它在尝试应用样式时遇到异常。

如果错误说明哪条线实际导致了错误,那么错误会更清楚,而不仅仅是暗示在网格的每一行中似乎存在任何问题。

如果您使用的是Visual Studio 2013 + ReSharper,它将使用蓝色波浪线为违规行加下划线,以指示错误的来源。为了记录,我与ReSharper没有任何关系,还有许多其他好的解决方案完成相同的工作,例如CodeRush,Visual Studio 2015等。