Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Preface: This article is part of a series focused on cross platform mobile app development (specifically Android and iOS) using the Alloy framework and Titanium Studio made by Appcelerator (https://www.appcelerator.com/). This article presumes a wor…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

919 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

13 Experts available now in Live!

Get 1:1 Help Now