Reputation: 806
I have a listbox having a list of items, and what I want is when I click on a specific item, I want to navigate to another page and also send the name of that particular item to the navigated page.
My xaml code:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ListBox x:Name="List" HorizontalAlignment="Left" Height="612" Margin="6,7,0,0" VerticalAlignment="Top" Width="443" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Width="400" Height="50" KeyUp="StackPanel_KeyUp_1">
<TextBlock x:Name="tbName" Width="200" Height="44" FontSize="22" FontWeight="Bold" Text="{Binding Name}"/>
<TextBlock x:Name="tbEmail" Width="200" Height="44" FontSize="22" Text="{Binding Email}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
In the following C# code, I've tried only to navigate to another page, but still it doesn't work,
private void StackPanel_KeyUp_1(object sender, KeyEventArgs e)
{
NavigationService.Navigate(new Uri("/Page2.xaml", UriKind.Relative));
}
How can I add keyup event handler for a row? And how should I send the corresponding name of the selected row?
Upvotes: 1
Views: 319
Reputation: 971
Instead of KeyUp, use Tap events and your code will work just fine.
XAML:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ListBox x:Name="List" HorizontalAlignment="Left" Height="612" Margin="6,7,0,0" VerticalAlignment="Top" Width="443" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Width="400" Height="50" Tap="StackPanel_Tap">
<TextBlock x:Name="tbName" Width="200" Height="44" FontSize="22" FontWeight="Bold" Text="{Binding Name}"/>
<TextBlock x:Name="tbEmail" Width="200" Height="44" FontSize="22" Text="{Binding Email}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
Handler: (Here toDoItem is the object type of the ListBox display and AnyUniqueParam is the parameter you want to pass
private void StackPanel_Tap(object sender, KeyEventArgs e)
{
ListBox listBox = (ListBox)sender;
ToDoItem toDoItemToOpen = listBox.SelectedItem as ToDoItem;
NavigationService.Navigate(new Uri("/NewTaskPage.xaml?pagedetails=" + toDoItemToOpen.AnyUniqueParam, UriKind.Relative));
}
Upvotes: 2