WPF DataGridRow.Item(" xx") - Option Strict On禁止后期绑定

时间:2015-08-03 19:14:51

标签: wpf vb.net datagrid late-binding option-strict

使用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禁止后期绑定"没有更正选项。 Option Strict Off可以正常使用。我已经尝试了以下所有内容来纠正它:

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

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

感谢任何输入,包括其他方式。

更新

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

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

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

dgRow.Item("xx").ToString

1 个答案:

答案 0 :(得分:1)

dgRow.ItemObject类型的属性。通过使用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方法可用。

相关问题