简体   繁体   中英

How to get data from a LongListSelector item in WP8

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

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.

您可以使用SelectionChanged事件和LongListSelector.SelectedItem属性来获取所选项目。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM