我想知道如何访问作为ListView成员的ListViewDataItem的数据键。
我觉得奇怪的是,在调试时可以访问DataKeysContainer,这可以在这里看到
然而,当在编码期间尝试访问DataKeyContainer时,这是不可能的,这也可以在这里看到
非常感谢有关如何访问ListViewDataItem的数据键及其值的任何建议。
答案 0 :(得分:0)
您可以使用后期绑定,即在Object
中填充您的循环变量。这允许您推迟成员验证,直到在运行时访问该成员。它类似于c#中的动态。
For Each itemObject As Object In lstViewModules.Items
' the DataKeysContainer you were looking for
Dim container = CType(itemObject.DataKeysContainer, Control)
' the same l in your loop
Dim l = CType(itemObject, ListViewDataItem)
' from here on, the rest of your loop code should work
Dim key As DataKey = lstViewModules.DataKeys(l.DataItemIndex)
Dim value = key("name")
Next
由于您知道它是ListView
,因此您还可以将container
投射到ListView
以方便设计时间。
注意:您必须Option Strict Off
才能使用此功能。