System.Windows.Data错误:4(每行) [英] System.Windows.Data Error: 4 (for every row)
问题描述
我的自定义数据网格类(每当使用时)每次加载一行时都会抛出此错误。 它是无害的,我倾向于忽略它,但我现在想要摆脱它。
My custom data grid class (whenever used) has been throwing this error everytime it loads a row. It's been harmless and I tend to ignore it, but I now want to get rid of it.
System.Windows.Data错误:4:无法找到与引用'RelativeSource FindAncestor绑定的源代码,AncestorType ='System.Windows.Controls.DataGrid',AncestorLevel ='1''。 BindingExpression:路径= AreRowDetailsFrozen;的DataItem = NULL; target元素是'DataGridDetailsPresenter'
(Name =''); target属性是'SelectiveScrollingOrientation'(类型'SelectiveScrollingOrientation')
System.Windows.Data错误:4:找不到用于绑定的源'RelativeSource FindAncestor,AncestorType ='System.Windows.Controls.DataGrid ',AncestorLevel ='1''。 BindingExpression:路径= HeadersVisibility;的DataItem = NULL; target元素是'DataGridRowHeader'
(Name =''); target属性是'Visibility'(类型'Visibility')
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.DataGrid', AncestorLevel='1''. BindingExpression:Path=AreRowDetailsFrozen; DataItem=null; target element is 'DataGridDetailsPresenter'
(Name=''); target property is 'SelectiveScrollingOrientation' (type 'SelectiveScrollingOrientation')
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.DataGrid', AncestorLevel='1''. BindingExpression:Path=HeadersVisibility; DataItem=null; target element is 'DataGridRowHeader'
(Name=''); target property is 'Visibility' (type 'Visibility')
我没有任何代码或xaml标记尝试创建或修改AreRowDetailsFrozen或HeadersVisibility的任何绑定,我知道 或者就此而言,DataGridDetailsPresentor和DataGridRowHeader。
I don't have any code or xaml markup attempting to create or modify any bindings to AreRowDetailsFrozen or HeadersVisibility, that I am aware of. Or for that matter, DataGridDetailsPresentor, and DataGridRowHeader.
这是datagrid的定义代码:
Here is the definition code for the datagrid:
Imports System.Windows.Controls.Primitives
Imports System.Data
Imports System.ComponentModel
Imports System.Reflection
Imports System.Windows.Controls
Imports System.Windows
Imports System.Windows.Data
Imports System.Windows.Input
Imports System.Windows.Media
Public Class ccMasterDataGrid
Inherits DataGrid
#Region "Properties"
Public WithEvents miSearch As MenuItem
Public WithEvents miGroup As MenuItem
Public WithEvents miUnFilter As MenuItem
Public WithEvents miQuickFilter As MenuItem
Public WithEvents miAddNew As MenuItem
Public WithEvents miCopyClip As MenuItem
Public WithEvents miUnsort As MenuItem
Public WithEvents miDelete As MenuItem
Public WithEvents ScrollViewer As ScrollViewer
Public FilterColumns As New List(Of DataColumn)
Public sortColumns As New List(Of DataColumn)
Public groupColumns As New List(Of DataColumn)
Private WithEvents mLCV As ListCollectionView = Nothing
Public Property LCV As ListCollectionView
Get
mLCV = LoadLCV()
Return mLCV
End Get
Set(ByVal value As ListCollectionView)
mLCV = value
End Set
End Property
Dim sortComp As New RowComparer
Public Event AddNew()
Public Event UserDelete(ByRef rows As List(Of Object))
Public Event RowGotFocus(row As DataGridRow)
Public Event RowLostFocus(row As DataGridRow)
Public Event VerticalScrollChanged(sender As Object, e As ScrollChangedEventArgs)
Public Event FilterModified(IsFiltered As Boolean)
Public Event RowsFiltered()
Public Event RowsSorted()
Private WithEvents dt As DataTable
Private isManualEditCommit As Boolean
Private isSettingGroups As Boolean
Public groupItemsExpanders As Dictionary(Of Object, Boolean)
#End Region
#Region "Load/Save"
Shared Sub New()
End Sub
Protected Overrides Sub OnInitialized(ByVal e As System.EventArgs)
MyBase.OnInitialized(e)
Name = "dgMaster"
AutoGenerateColumns = False
EnableRowVirtualization = True
Style = CType(Application.Current.FindResource("StandardDataGrid"), Style)
'set group style:
Dim datagridRowsPresenter As New FrameworkElementFactory(GetType(Primitives.DataGridRowsPresenter))
Dim itemsPanelTemplate As New ItemsPanelTemplate With {.VisualTree = datagridRowsPresenter}
Dim gStyle As New GroupStyle() With {.Panel = itemsPanelTemplate}
Me.GroupStyle.Add(gStyle)
gStyle.ContainerStyle = TryCast(Application.Current.FindResource("StandardGroupItem"), Style)
Me.ContextMenu = New ContextMenu
Me.ContextMenu.Items.Add(New MenuItem())
Me.ContextMenu.Items.Add(New MenuItem())
Me.ContextMenu.Items.Add(New MenuItem())
Me.ContextMenu.Items.Add(New MenuItem())
Me.ContextMenu.Items.Add(New MenuItem())
Me.ContextMenu.Items.Add(New MenuItem())
Me.ContextMenu.Items.Add(New MenuItem())
Me.ContextMenu.Items.Add(New MenuItem())
miSearch = Me.ContextMenu.Items(0)
miQuickFilter = Me.ContextMenu.Items(1)
miUnFilter = Me.ContextMenu.Items(2)
miUnsort = Me.ContextMenu.Items(3)
miGroup = Me.ContextMenu.Items(4)
miAddNew = Me.ContextMenu.Items(5)
miCopyClip = Me.ContextMenu.Items(6)
miDelete = Me.ContextMenu.Items(7)
miSearch.Style = CType(Application.Current.FindResource("SearchMenuItem"), Style)
miUnFilter.Style = CType(Application.Current.FindResource("UnFilterMenuItem"), Style)
miGroup.Style = CType(Application.Current.FindResource("GroupByMenuItem"), Style)
miQuickFilter.Style = CType(Application.Current.FindResource("QuickFilterMenuItem"), Style)
miAddNew.Style = CType(Application.Current.FindResource("AddNewMenuItem"), Style)
miCopyClip.Style = CType(Application.Current.FindResource("CopyClipMenuItem"), Style)
miUnsort.Style = CType(Application.Current.FindResource("UnSortMenuItem"), Style)
miDelete.Style = CType(Application.Current.FindResource("DeleteMenuItem"), Style)
Me.CanUserDeleteRows = False
End Sub
Public Overridable Sub dgMaster_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
Try
If Not (System.ComponentModel.DesignerProperties.GetIsInDesignMode(Me)) Then
LoadColumnsNames()
If Me.IsReadOnly Then
ContextMenu.Items.Remove(miAddNew)
ContextMenu.Items.Remove(miDelete)
End If
End If
ScrollViewer = GetScrollViewer(Me)
Catch ex As Exception
Dim eh As New ErrorHandler(ex)
eh.HandleIt()
End Try
End Sub
Public Function LoadLCV() As ListCollectionView
Dim newLCV As ListCollectionView = GetListCollectionView()
If newLCV IsNot Nothing Then
If TypeOf newLCV.SourceCollection Is DataView Then
Dim dv As DataView = newLCV.SourceCollection
dt = dv.Table
End If
End If
Return newLCV
End Function
Private Function GetListCollectionView() As ListCollectionView
If TypeOf CollectionViewSource.GetDefaultView(ItemsSource) Is ListCollectionView Then
Return DirectCast(CollectionViewSource.GetDefaultView(ItemsSource), ListCollectionView)
End If
Return Nothing
End Function
Public Sub LoadColumnsNames()
LoadDataGridGroupMenu()
For Each mi As MenuItem In miGroup.Items
AddHandler mi.Checked, AddressOf miGroup_Click
AddHandler mi.Unchecked, AddressOf miGroup_Click
Next
End Sub
Public Sub LoadDataGridGroupMenu()
FilterColumns = New List(Of DataColumn)
For Each column As DataGridColumn In Columns
If FilterColumns.Find(Function(x) x.Column Is column) Is Nothing Then
FilterColumns.Add(New DataColumn(column))
End If
Next
For Each gc As DataColumn In FilterColumns
Dim strcolumn As String = gc.Column.Header
If strcolumn <> String.Empty Then
Dim mi As New MenuItem
mi.Header = strcolumn
mi.IsCheckable = True
mi.IsChecked = False
Dim booFound As Boolean = False
For Each miEx As MenuItem In miGroup.Items
If miEx.Header = mi.Header Then
booFound = True
End If
Next
If booFound = False Then miGroup.Items.Add(mi)
End If
Next
End Sub
以下是数据网格的样式:
here are the styles for the data grid:
<Style x:Key="DataGridStandardCell" BasedOn="{StaticResource StandardFont}" TargetType="{x:Type DataGridCell}">
<Setter Property="Focusable" Value="True"/>
<Setter Property="FocusVisualStyle">
<Setter.Value>
<Style TargetType="Control">
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
<Setter Property="FontWeight" Value="ExtraBold"/>
</Style>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
<Setter Property="FontWeight" Value="ExtraBold"/>
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
<Setter Property="FontWeight" Value="ExtraBold"/>
</Trigger>
</Style.Triggers>
</Style>
<Style x:Key="DataGridCellRight" BasedOn="{StaticResource DataGridStandardCell}" TargetType="{x:Type DataGridCell}">
<Setter Property="TextBlock.TextAlignment" Value="Right" />
</Style>
<Style x:Key="DataGridCellCenter" BasedOn="{StaticResource DataGridStandardCell}" TargetType="{x:Type DataGridCell}">
<Setter Property="TextBlock.TextAlignment" Value="Center" />
</Style>
<Style x:Key="DataGridCellLeft" BasedOn="{StaticResource DataGridStandardCell}" TargetType="{x:Type DataGridCell}">
<Setter Property="TextBlock.TextAlignment" Value="Left" />
</Style>
<Style x:Key="StandardDataGrid" BasedOn="{StaticResource StandardFont}" TargetType="{x:Type DataGrid}">
<Setter Property="CellStyle" Value="{StaticResource DataGridCellLeft}"/>
<Setter Property="AlternatingRowBackground" Value="#FFF0F0F0"/>
<Setter Property="Margin" Value="0" />
<Setter Property="VirtualizingPanel.IsVirtualizingWhenGrouping" Value="True"/>
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#64007A33" />
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
<SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Black" />
</Style.Resources>
</Style>
<Style TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
<Setter Property="FontWeight" Value="ExtraBold"/>
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
<Setter Property="FontWeight" Value="ExtraBold"/>
</Trigger>
</Style.Triggers>
<Setter Property="Focusable" Value="True"/>
<Setter Property="FocusVisualStyle">
<Setter.Value>
<Style TargetType="Control">
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
<Setter Property="FontWeight" Value="ExtraBold"/>
</Style>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="FocusedDataGridRow" TargetType="{x:Type DataGridRow}">
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
<Setter Property="FontWeight" Value="ExtraBold"/>
</Style>
<Style x:Key="DataGridCellDatePicker" BasedOn="{StaticResource StandardFont}" TargetType="{x:Type DatePicker}" >
<Setter Property="Margin" Value="0"/>
<Setter Property="Padding" Value="1"/>
<Setter Property="BorderThickness" Value="0"/>
</Style>
<Style x:Key="StandardGroupItem" TargetType="{x:Type GroupItem}">
<Setter Property="Margin" Value="0,0,0,0"/>
<Setter Property="MinHeight" Value="0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander Name="part_Expander" Background="#87007000" BorderBrush="#C5003800" Foreground="Black" BorderThickness="1,1,1,1" Margin="3" Padding="1" MinHeight="0" >
<!--IsExpanded="{Binding Path=ExpandGroups, RelativeSource={RelativeSource AncestorType=TTSMain:ccMasterDataGrid}}"-->
<Expander.Header>
<DockPanel>
<TextBlock Style="{StaticResource StandardFont}" FontWeight="Bold" Text="{Binding Path=ItemCount}" Width="Auto"/>
<TextBlock Style="{StaticResource StandardFont}" FontWeight="Bold" Text=" : " Width="Auto"/>
<TextBlock Style="{StaticResource StandardFont}" FontWeight="Bold" Text="{Binding Path=Name}" Width="Auto"/>
</DockPanel>
</Expander.Header>
<Expander.Content>
<ItemsPresenter />
</Expander.Content>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
任何线索?
Any clues?
jvj
推荐答案
嗨Jamie,
Hi Jamie,
请您分享完整的代码,这可以重现我们这边的问题。
Could you please share a complete code, which could reproduce the issue on our side.
此外,您可以查看以下博客,这些博客提供了两种解决问题的解决方案。
In addition, you could check the following blogs, which provide two solution to resolve the issue.
https://weblogs.asp.net/akjoshi/resolving-un-harmful-binding-errors-in-wpf
祝你好运,
张龙
这篇关于System.Windows.Data错误:4(每行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!