【问题标题】:Getting Selected Item information from ListView in WinRT从 WinRT 中的 ListView 获取选定项信息
【发布时间】:2012-10-23 21:47:50
【问题描述】:

我有以下代码使用 Flickr 中的照片填充 ListView

 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;
    }

然后我希望能够从此 ListView 中获取单个项目的源数据以在其他地方使用。但是,我似乎无法使用某些命令到达任何地方。我对 C# 还不够陌生,我不知道是否应该使用 SelectedItems、Items 或 SelectedIndex 方法来查找我的照片存储在哪个节点中。

任何帮助都会很棒。

【问题讨论】:

    标签: c# listview windows-runtime


    【解决方案1】:

    你可以使用这段代码:

    已编辑: 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());
    

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

    【讨论】:

    • 这是在正确的轨道上,但我仍然需要能够获取我在 UI 中选择的项目的索引。所以我有一个照片列表,用户点击第三张照片我需要获取第三张照片的索引。
    • 只是想知道以这种方式定义我的列表视图是否有效'FlickrListView.ItemsSource = photos;'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 2016-08-19
    • 1970-01-01
    • 2018-01-28
    • 2019-08-07
    • 2020-07-05
    相关资源
    最近更新 更多