我有一個簡單datatgrid當我在下面的方式定義它的作品:WPF的Datagrid綁定錯誤時不自動生成列
<DataGrid
ItemsSource="{Binding EmployeeCollectionViewSource.View}"
Style="{DynamicResource FTC_DataGridStyle}" AutoGenerateColumns="True" />
如果我刪除的AutoGenerateColumns =「真」,並試圖定義我列如下,我得到一個錯誤:
<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>
我得到的錯誤是:
{"'Add value to collection of type 'System.Windows.Controls.ItemCollection' threw an exception.' Line number '31' and line position '32'."} {"Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead."}
我使用MVVM模式和EmployeeCollectionViewSource的綁定是從實體framemowrk生成的ObservableCollection中填充的collectionviewsource。
我已經嘗試刪除列和雙重檢查綁定名稱,我無法弄清楚這個錯誤來自哪裏。輸出窗口中沒有顯示錯誤。
問題 你能幫我解決這個錯誤,所以我可以手動定義我的列嗎?
額外的細節: 以下是我的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
如果您從xaml中一次刪除一個DataColumn,您是否永遠不會收到該錯誤?我懷疑你的一列是問題所在。如果是這樣,哪一列是問題。 – 2013-03-18 22:05:18
@DJBurb我試着一次刪除一列,它們都拋出錯誤 – 2013-03-19 01:33:18
你在哪裏設置你的DataContext? – 2013-03-19 01:43:45