我有一个简单的datatgrid,当我按以下方式定义它时可以工作:
<DataGrid
ItemsSource="{Binding EmployeeCollectionViewSource.View}"
Style="{DynamicResource FTC_DataGridStyle}" AutoGenerateColumns="True" />
如果我删除AutoGenerateColumns =“True”并尝试按如下方式定义我的列,则会收到错误消息:
<DataGrid
ItemsSource="{Binding EmployeeCollectionViewSource.View}"
Style="{DynamicResource FTC_DataGridStyle}" >
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding idCertification}" Header="ID" Width="50" IsReadOnly="True" CellStyle="{DynamicResource IDCellStyle}"/>
<DataGridTextColumn Binding="{Binding chrTitle}" Header="TITLE" Width="130" CellStyle="{DynamicResource TextCellStyle}"/>
<DataGridTextColumn Binding="{Binding chrDetail}" Header="DETAIL" Width="300" CellStyle="{DynamicResource TextCellStyle}"/>
<DataGridTextColumn Binding="{Binding chrProvider}" Header="PROVIDER" Width="130" CellStyle="{DynamicResource TextCellStyle}"/>
</DataGrid.Columns> />
</DataGrid>
我得到的错误是:
{“'为类型集合添加值 'System.Windows.Controls.ItemCollection'引发了一个异常。线 数字'31'和行位置'32'。“} {”操作无效时 ItemsSource正在使用中。使用访问和修改元素 ItemsControl.ItemsSource代替。“}
我使用MVVM模式,EmployeeCollectionViewSource的绑定是一个collectionviewsource,它是从实体framemowrk生成的ObservableCollection中填充的。
我尝试删除列并仔细检查绑定名称,我无法弄清楚此错误的来源。输出窗口中没有显示错误。
问题 您能否帮我解决此错误,以便我可以手动定义我的列?
其他详细信息 以下是我的viewmodel类:
Public Class EmployeeCertificationViewModel
Inherits ViewModelBase
#Region "DECLARATIONS"
Public Const CertificationCollectionPropertyName As String = "EmployeeCertifications"
Public Const EmployeeCollectionViewSourcePropertyName As String = "EmployeeCollectionViewSource"
''this is a holder for the employee data service
Private _EmployeeAccess As IEmployeeDataService
Private _EmployeeCertifications As New ObservableCollection(Of certification)
Private _EmployeeCollectionViewSource As New CollectionViewSource
''tracks if employee validation is coming from navigation or listview selecteditemchanged
Private FlagNavigating As Boolean = False
Private _NavigationService As INavigationService
Private _ModelService As IModelService
Private Context As FTC_Context
#End Region
#Region "PROPERTIES"
Public Property EmployeeCertifications As ObservableCollection(Of certification)
Get
Return Me._EmployeeCertifications
End Get
Set(ByVal value As ObservableCollection(Of certification))
Me._EmployeeCertifications = value
RaisePropertyChanged(CertificationCollectionPropertyName)
End Set
End Property
Public Property EmployeeCollectionViewSource As CollectionViewSource
Get
Return Me._EmployeeCollectionViewSource
End Get
Set(value As CollectionViewSource)
If _EmployeeCollectionViewSource Is value Then
Return
End If
_EmployeeCollectionViewSource = value
RaisePropertyChanged(EmployeeCollectionViewSourcePropertyName)
End Set
End Property
#End Region
#Region "COMMANDS"
#End Region
#Region "METHODS"
#End Region
#Region "CONSTRUCTOR"
Public Sub New(NavService As INavigationService, EmployeeService As IEmployeeDataService, ModelService As IModelService)
_ModelService = ModelService
Context = _ModelService.NewContext
_NavigationService = NavService
_EmployeeAccess = EmployeeService
EmployeeCertifications = EmployeeService.Get_Certification(Context)
EmployeeCollectionViewSource.Source = EmployeeCertifications
End Sub
#End Region
End Class
答案 0 :(得分:3)
DataGrid.AutoGenerateColumns Property true 。如果要定义自己的列,则必须明确将其设置为false。否则,您将同时拥有两种列类型(自动生成和自定义)。
<DataGrid ItemsSource="{Binding}" AutoGenerateColumns="False">
<DataGrid.Columns>
...
</DataGrid.Columns>
</DataGrid>
但您的真正问题似乎是代码中/>
之后的其他 </DataGrid.Columns>
。删除它,异常应该消失。