Reputation: 568
So I have a LongListSelector that will soon be bound to a list of object.
What I want is when one of the LongListSelectorItems is tapped that I get the specific object tapped, and have the ability to pass that object to another screen so I can show full information about the object
private void PeopleList_Tap_1(object sender, System.Windows.Input.GestureEventArgs e)
{
MessageBox.Show("SUCCESS");
}
My Tap is working, I just don't know how to get the object that was selected, or how to pass it to another page using NavigationService
Upvotes: 1
Views: 5275
Reputation: 386
Below is pulled straight from my latest app.
You can't directly pass an object, but you can pass text data. In my case, an id:
private void WishListBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (WishListBox != null && WishListBox.SelectedItem != null)
{
var selectedItem = (Models.Gift)WishListBox.SelectedItem;
WishListBox.SelectedIndex = -1;
var id = selectedItem.Id;
NavigationService.Navigate(new Uri("/Views/Gift/GiftView.xaml?action=load&id=" + id, UriKind.Relative));
}
}
then on the receiving end:
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if (NavigationContext.QueryString.ContainsKey("action"))
{
if (NavigationContext.QueryString["action"]=="load")
{
PageTitle.Text = "edit gift";
giftVm.Gift = App._context.Gifts.Single(g => g.Id == Int32.Parse(NavigationContext.QueryString["id"]));
}
else if (NavigationContext.QueryString["action"] == "new")
{
PageTitle.Text = "new gift";
}
else if (NavigationContext.QueryString["action"] == "newWishList")
{
App.vm = ((MainViewModel)App.vm).Me;
}
}
else
{
MessageBox.Show("NavigationContext.QueryString.ContainsKey('action') is false");
}
}
In my case, the data is stored in a DB. I simply cast the selected item to the correct object type, then check its ID and pass that to the next page where I do a lookup.
I hope this helps.
Upvotes: 3
Reputation: 1517
You can use SelectionChanged
event and LongListSelector.SelectedItem
property to get selected item.
Upvotes: 2