2013-03-18 51 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