0
votes

I have a LongListSelector in an .xaml and I am able to fill it by binding to a an ItemSource when the source is filled by a DataContext using a single table from my SQL Server CE database like this:

    Dim row = (From rows In db.Hub
               Order By rows.HubID Descending
               Select rows).ToList()

    Me.MainLongListSelector.ItemsSource = row

I am thus able to get the ID of the selected item as follows:

HubID = CType(MainLongListSelector.SelectedItem, Hub).HubID

I am also able to bind to a 'query' DataSource as follows:

    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

however, since this is not referring to a specific table in the DataContext, I cannot get the ID using the above code, ie:

Dim ActID = CType(MainLongListSelector.SelectedItem, Activity).ActivityID '- returns nothing

How should I get the value(s) of selectedItem in this case?

NB: I have created the anonymous fields (.ID and .Title) because those are the names I have bound in the xaml, so the LongListSelected gets populated without writing extra code.

Thanks

1

1 Answers

0
votes

Phew!!

I discovered that two things:

  1. this HubID = CType(MainLongListSelector.SelectedItem, Hub).HubID is calling a List (Of DataContext), while in the second scenario above I am using a List (Of Anonymous). So I searched for List (Of Anonymous) and this came up!
  2. I now know I can create a class for List (Of Anonymous) and properly name its properties, thus make it available outside its methods, like in my 'query' question above.

So the answer is I created the class for my anonymous list, declared its properties

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

and therefore assigned them to the ItemSource values,

Select New AnonList With {.ID = ac.ActivityID,

thus being able to get the SelectedItem values as required:

ActivityID = CType(MainLongListSelector.SelectedItem, AnonList).ID

Took a bit of determination to figure that out!