2013-03-18 80 views
0

我有一個簡單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 
+0

如果您從xaml中一次刪除一個DataColumn,您是否永遠不會收到該錯誤?我懷疑你的一列是問題所在。如果是這樣,哪一列是問題。 – 2013-03-18 22:05:18

+0

@DJBurb我試着一次刪除一列,它們都拋出錯誤 – 2013-03-19 01:33:18

+0

你在哪裏設置你的DataContext? – 2013-03-19 01:43:45

回答

3

DataGrid.AutoGenerateColumns Property真正默認。如果你想定義你自己的列,你必須明確地將它設置爲false。否則,您將同時擁有兩種列類型(自動生成和自定義)。

<DataGrid ItemsSource="{Binding}" AutoGenerateColumns="False"> 
    <DataGrid.Columns> 
     ... 
    </DataGrid.Columns> 
</DataGrid> 

但你真正的問題似乎是在你的代碼的附加/></DataGrid.Columns>。刪除它,異常應該消失。

+0

感謝您的答覆,我添加了AutoGenerateColumns =「False」,但錯誤仍然完全一樣,因爲它是 – 2013-03-19 01:30:41

+0

我最終得到我的解決方案工作,我不需要顯式設置AutoGenerateColumns爲false,如果我手動設置它似乎認識到, – 2013-03-19 17:31:08

+0

你是部分正確的,我想我也發現了這個問題。請看我更新的答案。 – LPL 2013-03-19 19:54:48