Getting Selected Item information from ListView in WinRT

China☆狼群 提交于 2019-12-13 07:06:07

问题


I have the following code which popualtes a ListView with photos from Flickr

 private async void ParseFlickrResponse(HttpResponseMessage response)
    {
        XDocument xml = XDocument.Parse(await response.Content.ReadAsStringAsync());          
        var photos = from results in xml.Descendants("photo")
                     select new FlickrImage
                     {
                         ImageId = results.Attribute("id").Value.ToString(),
                         FarmId = results.Attribute("farm").Value.ToString(),
                         ServerId = results.Attribute("server").Value.ToString(),
                         Secret = results.Attribute("secret").Value.ToString(),
                         Title = results.Attribute("title").Value.ToString()
                     };

        FlickrListView.ItemsSource = photos;
    }

I want to be able to then get source data for a individual item from this ListView to use elsewhere. However I can't seem to get anywhere with some of the commands. I'm new enough to C# and I don't know whether I should be using the SelectedItems, Items or SelectedIndex method to find which node my photo is stored in.

Any help would be great.


回答1:


You can use this piece of code:

EDITED: with SelectedItems

Dictionary<string, List<string>> dict =
    FlickrListView.SelectedItems
            .Cast<ListViewItem>()
            .ToDictionary(
                item => item.Text,
                item => item.SubItems
                            .Cast<ListViewItem.ListViewSubItem>()
                            .Select(subItem => subItem.Text)
                            .ToList());

OR

foreach (var item in FlickrListView.SelectedItems)
{
    FlickrImage obj = (FlickrImage) item;
    // ... do something ...
}


来源:https://stackoverflow.com/questions/13031465/getting-selected-item-information-from-listview-in-winrt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!