gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 10:55:29 32 4
gpt4 key购买 nike

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

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com