Link to home
Start Free TrialLog in
Avatar of jsmithr
jsmithrFlag for United States of America

asked on

How to access Selected Item 'Tag' from Windows Phone Listbox bound by ItemSource

When I 'hard code' the ListItems inside the ListBox I am able to access the Tag property during a SelectionChanged. But when I bind the ListBox.ItemSource I am unable to access the Tag Property.

First, I do this:
    Private Sub GetPlayers(sender As Object, e As ServiceReference1.GetPlayersCompletedEventArgs)
        lbAvailablePlayers.ItemsSource = e.Result
    End Sub

Open in new window

                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <ListBoxItem Content="{Binding FullName}" Tag="{Binding PlayerID}" Padding="0,5,0,5"></ListBoxItem>
                        </DataTemplate>
                    </ListBox.ItemTemplate>

Open in new window


Then, I try this:
    Private Sub lbAvailablePlayers_SelectionChanged(sender As System.Object, e As System.Windows.Controls.SelectionChangedEventArgs)

        Dim sel = DirectCast(lbAvailablePlayers, ListBox).SelectedItem
        Dim strPlayerID As String = DirectCast(sel, ListBoxItem).Tag
        NavigationService.Navigate(New Uri("/EnterScores.xaml?Opponent=" & strPlayerID, UriKind.Relative))

    End Sub

Open in new window


But then I get this:
System.InvalidCastException was unhandled
  Message=InvalidCastException
  StackTrace:
       at FSPIRBWP.SubmitGame.lbAvailablePlayers_SelectionChanged(Object sender, SelectionChangedEventArgs e)
       at System.Windows.Controls.Primitives.Selector.OnSelectionChanged(SelectionChangedEventArgs e)
       at System.Windows.Controls.Primitives.Selector.InvokeSelectionChanged(List`1 unselectedItems, List`1 selectedItems)
       at System.Windows.Controls.Primitives.Selector.SelectionChanger.End()
       at System.Windows.Controls.Primitives.Selector.SelectionChanger.SelectJustThisItem(Int32 oldIndex, Int32 newIndex)
       at System.Windows.Controls.ListBox.MakeSingleSelection(Int32 index)
       at System.Windows.Controls.ListBox.HandleItemSelection(ListBoxItem item, Boolean isMouseSelection)
       at System.Windows.Controls.ListBox.OnListBoxItemClicked(ListBoxItem item)
       at System.Windows.Controls.ListBoxItem.OnManipulationCompleted(ManipulationCompletedEventArgs e)
       at System.Windows.Controls.Control.OnManipulationCompleted(Control ctrl, EventArgs e)
       at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)
  InnerException: 

Open in new window


But I can see this:
User generated image
So, is there something I am missing about binding the listbox via an ItemsSource?
I mean, if I know the SelectedItem, should I not be able to 'parse' out the 'Tag' Property [which contains the string i want]?

And as far as the screen shot I posted, I find it interesting that all of the properties associated with the SelectedItem are accessible during the SelectionChanged Event even though I did not assign all of them to the ListBoxItem. I did try to write some code to access that information, but I was unsuccessful.

I guess my questions is this:
Once I build the ListBox via an ItemsSource Bind, how do I access the Tag Property of the Selected Item during the SelectionChanged Event?

Jason
Avatar of jsmithr
jsmithr
Flag of United States of America image

ASKER

Hmmm... This seemed to work. Pretty cool, I guess.
        Dim sel = DirectCast(lbAvailablePlayers, ListBox).SelectedItem
        Dim strPlayerID = DirectCast(sel, FSPIRBWP.ServiceReference1.FSPIRBServicestrctPlayers).PlayerID

Open in new window


Its not how I would have thought it should work.
I was thinking that I had to Bind to the Tag Property, then access that Tag Property on the SelectionChanged Event...

Kind of like in asp.Net when you have a Drop Down List with a Value Property and you grab the Value of the Selected Item in code behind.

Ah well, hope I didnt waste anyones time.
Jason
ASKER CERTIFIED SOLUTION
Avatar of Mikal613
Mikal613
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial