不自动生成列时出现 WPF Datagrid 绑定(bind)错误

标签 wpf data-binding mvvm datagrid

我有一个简单的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>

我得到的错误是:

{"'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 的绑定(bind)是一个 collectionviewsource,它是从实体 framemowrk 生成的 ObservableCollection 填充的。

我尝试删除列并仔细检查绑定(bind)名称,但我无法弄清楚这个错误来自哪里。输出窗口中没有显示错误。

问题
你能帮我解决这个错误,以便我可以手动定义我的列吗?

其他详细信息:
以下是我的 View 模型类:
    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

最佳答案

DataGrid.AutoGenerateColumns Property真实 默认情况下。如果要定义自己的列,则必须明确将其设置为 false。否则,您将同时拥有两种列类型(自动生成和自己定义)。

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

但你真正的问题似乎是额外的 /> </DataGrid.Columns> 之后在你的代码中。删除它,异常应该消失。

关于不自动生成列时出现 WPF Datagrid 绑定(bind)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15487658/

相关文章:

wpf - 在 F# 中访问 XAML (WPF) 元素

c# - 如何停止 MahApps.Metro 覆盖基本控件的样式

wpf - 如何显示绑定(bind)数据网格的值

android - 使用数据绑定(bind)连接两个动态字符串

c# - 使用 XamlReader.Parse() 从字符串读取 xaml 时使用转换器

WPF MVVM 动态子菜单绑定(bind)问题

gradle - kapt:发生异常:java.lang.NoSuchMethodError:android.databinding.tool.store.ResourceBundle.<init>(Ljava/lang/String;Z)V

c# - 在 WPF 中同步 ViewModel

c# - 我可以将 RoutedCommand 绑定(bind)到 WPF 中的命令吗?

wpf - 动态绑定(bind)到 Window 的 MenuItem 上的 ViewModel 命令