Solved

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

Posted on 2012-03-23
2
1,234 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JSON Deserialize issue 6 40
moto 4g plus phone apps 5 50
How to get a Powershell script to launch from Visual Studio 20 67
Adjust the codes 3 35
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…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

726 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