Question

I am creating a Windows Phone 8 App and I did a LongListSelector for Players in the app

LongListSelector

 <phone:LongListSelector x:Name="playersLongList" LayoutMode="List" IsGroupingEnabled="False" HorizontalAlignment="Left" Height="653" Margin="15,190,0,0" VerticalAlignment="Top" Width="412" SelectionChanged="playersLongList_SelectionChanged">
            <phone:LongListSelector.ItemTemplate>
                <DataTemplate>
                    <StackPanel x:Name="s1">
                        <TextBlock x:Name="playerName" Text="{Binding FirstName}" FontFamily="/Assets/Fonts/Moire Light.ttf#Moire Light" FontSize="48" />
                    </StackPanel>
                </DataTemplate>
            </phone:LongListSelector.ItemTemplate>
        </phone:LongListSelector>

Now, what i need is when the player taps on an item, the app shows a MessageBox that shows the Player Name of the Selected Item , and this is how i did it in the EventHandler

 private void playersLongList_SelectionChanged(object sender, SelectionChangedEventArgs e)
 {
     var tb = sender as TextBlock;
     string fname = tb.Text;
     MessageBox.Show("hello"+fname);
 }

the debug shows NullReferenceException at string fname = tb.Text;

What did i do wrong ?

Was it helpful?

Solution

Try this.

private void playersLongList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (e.AddedItems.Count > 0)
    {
        Player p = e.AddedItems[0] as Player;
        string fname = p.FirstName;
        MessageBox.Show("hello"+fname);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top