Solved

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

Posted on 2012-03-23
2
1,239 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As the title indicates, I have done this before. It chills me everytime I update the OS on my phone, (http://www.experts-exchange.com/articles/18084/Upgrading-to-Android-5-0-Lollipop.html) because one time I did this and I essentially had a bricked …
Once again I push the limits of my phone.  An introduction to the Android Google Now Launcher.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

718 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