C# WPF错误:";System.Windows.Markup.XamlParseException:';DataGridRow';TargetType与元素的类型不匹配';GridRowContent'&引用;

C# WPF错误:";System.Windows.Markup.XamlParseException:';DataGridRow';TargetType与元素的类型不匹配';GridRowContent'&引用;,c#,.net,wpf,visual-studio-2013,C#,.net,Wpf,Visual Studio 2013,此问答对属于“分享知识,问答式”类型,旨在将此错误与最可能的原因联系起来 我希望它将来对一些开发者有用 错误: System.Windows.Markup.XamlParseException: 'DataGridRow' TargetType does not match type of element 'GridRowContent'. ---> System.InvalidOperationException: 'DataGridRow' TargetType does not ma

此问答对属于“分享知识,问答式”类型,旨在将此错误与最可能的原因联系起来

我希望它将来对一些开发者有用

错误:

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)
System.Windows.Markup.XamlParseException:“DataGridRow”TargetType与元素“GridRowContent”的类型不匹配。-->System.InvalidOperationException:“DataGridRow”TargetType与元素“GridRowContent”的类型不匹配。
位于System.Windows.Style.CheckTargetType(对象元素)
在System.Windows.StyleHelper.UpdateStyleCache(FrameworkElement fe、FrameworkContentElement fce、Style oldStyle、Style newStyle、Style&styleCache)
在System.Windows.FrameworkElement.OnStyleChanged(DependencyObject d,DependencyPropertyChangedEventArgs e)
位于System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
位于System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
位于System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs参数)
在System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex EntryIndex,DependencyProperty dp,PropertyMetadata,EffectiveValueEntry oldEntry,EffectiveValueEntry&newEntry,布尔强制WithDeferredReference,布尔强制WithCurrentValue,OperationType OperationType)
在System.Windows.StyleHelper.ApplyTemplatedParentValue(DependencyObject容器、FrameworkObject子对象、Int32子索引、FrugalStructList`1和childRecordFromChildIndex、DependencyProperty dp、FrameworkElementFactory templateRoot)
在System.Windows.StyleHelper.InvalidatePropertiesOnTemplateNode(DependencyObject容器、FrameworkObject子对象、Int32子索引、FrugalStructList`1和childRecordFromChildIndex、Boolean isDetach、FrameworkElementFactory templateRoot)
位于System.Windows.FrameworkTemplate.InvalidatePropertiesOnTemplate(DependencyObject容器,Object currentObject)
位于System.Windows.FrameworkTemplate.HandleBeforeProperties(对象createdObject、DependencyObject和rootObject、DependencyObject容器、FrameworkElement feContainer、INameScope名称范围)
在System.Windows.FrameworkTemplate.c__DisplayClass6.b__3(对象发送方,XamlObjectEventArgs args args)

当我遇到这个特殊错误时,根本原因是网格中的样式不兼容

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

<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相关的行相关。找不到指向的行样式,或者该网格的行样式类型错误。当WPF向上搜索可视化树时,它在尝试应用样式时遇到异常

如果它说是哪一行导致了错误,而不是仅仅暗示网格中的每一行似乎都有问题,那么错误就会更清楚

如果您使用的是Visual Studio 2013+ReSharper,它将在有问题的行下加一条蓝色波浪线,指示错误的来源。为了记录在案,我与ReSharper没有任何关系,还有许多其他好的解决方案可以做同样的工作,比如CodeRush、Visual Studio 2015等