我在.xaml中有一个LongListSelector,当我使用SQL Server CE数据库中的单个表格填充DataContext时,我可以通过绑定到ItemSource来填充它:
Dim row = (From rows In db.Hub
Order By rows.HubID Descending
Select rows).ToList()
Me.MainLongListSelector.ItemsSource = row
我因此能够获得所选项目的ID,如下所示:
HubID = CType(MainLongListSelector.SelectedItem, Hub).HubID
我也可以按如下方式绑定到'查询'DataSource:
Dim row = (From ac In db.Activity
Join at In db.ActivityType On ac.ActivityTypeID Equals at.ActivityTypeID
Select New With {.ID = ac.ActivityID,
.Title = ac.Activity1}).ToList()
Me.MainLongListSelector.ItemsSource = row
但是,由于这不是指DataContext中的特定表,我无法使用上面的代码获取ID,即:
Dim ActID = CType(MainLongListSelector.SelectedItem, Activity).ActivityID '- returns nothing
在这种情况下,我应该如何获得selectedItem的值?
注意:我创建了匿名字段(.ID和.Title),因为这些是我在xaml中绑定的名称,因此不会编写额外的代码就会填充LongListSelected。
由于
答案 0 :(得分:0)
呼!!
我发现了两件事:
所以答案是我为我的匿名列表创建了类,声明了它的属性
Public Class AnonList
Private _id As Integer
Public Property ID() As Integer
Get
Return _id
End Get
Set(ByVal value As Integer)
_id = value
End Set
End Property
Private _title As String
Public Property Title() As String
Get
Return _title
End Get
Set(ByVal value As String)
_title = value
End Set
End Property
Private _desc As String
Public Property Desc() As String
Get
Return _desc
End Get
Set(ByVal value As String)
_desc = value
End Set
End Property
End Class
因此将它们分配给ItemSource值
Select New AnonList With {.ID = ac.ActivityID,
因此能够根据需要获取SelectedItem值:
ActivityID = CType(MainLongListSelector.SelectedItem, AnonList).ID
有点决心解决这个问题!