Solved

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

Posted on 2012-03-23
2
1,196 Views
Last Modified: 2014-11-12
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:
Selected Item Data
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
0
Comment
Question by:jsmithr
2 Comments
 

Author Comment

by:jsmithr
ID: 37759680
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
0
 
LVL 48

Accepted Solution

by:
Mikal613 earned 500 total points
ID: 37761908
When you are binding controls all you have to remember is to cast the Selected item as the control that is calling the selected index event.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

If you are anything like me, you install many apps on your phone and have your life on it, sometimes literally.  When I bought my current phone, a Samsung Galaxy S5 from Verizon, they were only selling the versions with 16 GB internal.  I didn't rea…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This video discusses moving either the default database or any database to a new volume.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now