WPF DataGridRow.Item("xx") - Option Strict On 不允许后期绑定

WPF DataGridRow.Item("xx") - Option Strict On disallows late binding

使用 DataGrid 的 WPF 应用程序。用户双击一个单元格,我需要获取该行中另一个单元格的值。

Dim dep As DependencyObject = DirectCast(e.OriginalSource, DependencyObject)
Dim dgRow As DataGridRow = Nothing
While dep IsNot Nothing
    If TypeOf dep Is DataGridRow Then
        dgRow = DirectCast(dep, DataGridRow)
    End If
    dep = VisualTreeHelper.GetParent(dep)
End While

所以现在,我有了行,我想从特定列中获取值:

Dim xx As String = dgRow.Item("xx")

这让我 "Option Strict On disallows late binding" 没有更正选项。它适用于 Option Strict Off。我已经尝试了以下所有方法来更正它:

dgRow.Item("xx").ToString
DirectCast(dgRow.Item("xx"), String)
CType(dgRow.Item("xx"), String)

但是,在所有这些情况下,红色波浪线仍然在 dgRow.Item("xx") 下方。

感谢任何意见,包括解决此问题的替代方法。

更新

这是最终有效的代码。我查看了 Item 属性 的类型,它是 DataRowView。感谢下面马克的回答。

dgRow = DirectCast(DirectCast(dep, DataGridRow).Item, DataRowView)

这使我能够在没有后期绑定错误的情况下执行此操作:

dgRow.Item("xx").ToString

dgRow.Item 是类型 Object 的 属性。通过使用 dgRow.Item("xx"),您正在尝试调用默认值 属性,而 Object 不存在,因此会出现您所看到的错误。

("xx") 部分来看,该行似乎绑定到某种字典。如果是这种情况,您需要在从中访问值之前将 dgRow.Item 转换为适当的类型,例如

Dim xx As String = DirectCast(dgRow.Item, Dictionary(Of String, String))("xx")

更新

再次阅读,看起来您可能绑定到 DataTable,在这种情况下,每一行都将绑定到 DataRow,所以您可能需要这样的东西:

Dim xx As String = DirectCast(dgRow.Item, DataRow).Field(Of String)("xx")

请注意,您可能需要添加对 System.Data.DataSetExtensions.dll 的引用才能使 Field 方法可用。