如何访问ListView的Items值 [英] How to access ListView's Items values

查看:128
本文介绍了如何访问ListView的Items值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我无法找到任何特定于Windows 8 / 8.1的内容,因为我正在Windows商店应用模板中执行此项目。



我有得到2个列表视图我将删除并在两者之间添加值,我目前遇到的问题是当我尝试从我要添加项目的列表中检索项目及其值时。



我能够使用for循环来查找列表视图的项目集合中的许多项目,但这似乎是我能做的全部,我似乎无法使用foreach循环(这使得这更令人讨厌),因为它导致应用程序在搜索列表视图中的项目时简单崩溃。



所以我的问题是(最后):我如何访问已添加到ListView.Items,ItemCollection的项目,并检索所述项目属性及其值(即名称,地址,电话号码......无论这可能是什么)



我花了相当多的时间在这上面,这令人沮丧,我虽然使用lambda表达式,但不是100%肯定在这种情况下实际上可以工作。



感谢您提供的任何帮助。

解决方案

对于评论的人,谢谢,对不起有点事情......对于代码不太详细部分,我无法显示此代码,因为我没有尝试检索项目,因为我一直没有这样做。但是,在我放弃我的伴侣上班后,今天早上我想出了这个。



这是我的代码解决方案:

  private   async   void  btnSave_Click( object  sender,RoutedEventArgs e)
{
try
{
sortedItems = < span class =code-keyword> new List< favouritesitems>();
for int i = 0 ; i < lvFavourites.Items.Count(); i ++)
{
lvFavourites.SelectedIndex = i;
FavouritesItems item =(FavouritesItems)lvFavourites.SelectedItem;
sortedItems.Add( new FavouritesItems(item));
}
等待 SaveObjectToXml< list>< favouritesitems>>(sortedItems, favouritelelistItems.xml);
MessageDialog msg = new MessageDialog( 收藏夹已成功保存到文件);
await msg.ShowAsync();
}
catch (例外情况)
{
MessageDialog msg = MessageDialog( 发生未知错误: + ex.ToString());
await msg.ShowAsync();
}
} < / favouritesitems > < / list > < / favouritesitems >





我要做的是在我的FavouritesItems类中添加另一个构造函数(这是要添加到的对象) ListView.Items项目集合)



这是我的FavouritesItems类:



  public   class  FavouritesItems 
{
私人 FavouritesItems selectedItem;

public string 部分{获得; set ; }
public string 目的地{ get ; set ; }

public FavouritesItems( string section, string destination)
{
this .Section = section;
this .Destination = destination;
}

public FavouritesItems()
{
}

< span class =code-keyword> public
FavouritesItems(FavouritesItems selectedItem)
{
this .Section = selectedItem.Section;
this .Destination = selectedItem.Destination;
}
}





使用这个新的构造函数我能够获取一个完整的项目(包括目的地单独添加为字符串,即ListView.Items.Add(new FavouritesItems(Sight-Seeing,Melbourne);等)并且能够专门和单独检索属性值。



考虑到所有这些都在FOR循环中,我能够为列表中的每个项目执行此操作..一次一个。确保selectedIndex等于FOR循环整数i。



希望这可以帮助任何可能遇到类似问题的人在使用Windows Store应用程序时使用ListView和他们的ItemCollections。



注意:我知道第一个代码段有点......奇怪的格式..不知道如何解决它。(从visual studio发布到代码中的代码,它最终看起来像它看起来)


So i haven't been able to find anything specific to Windows 8/8.1 as i am doing this project in the Windows store apps template.

I have got 2 list views of which i am removing and adding values between the two, the issue i am currently having is when attempting to retrieve the items and their values from the list that i am adding items to.

I am able to use a for loop to find out how MANY items are in the item collection of the listview, however that seems to be all i can do, I can't seem to use a foreach loop (which makes this that much more annoying) as it causes the app to simply crash when it comes to searching for the items in the listview.

So my question is (finally): How do i access the items that have been added to the ListView.Items, ItemCollection and retrieve said items properties and their values (i.e the name, the address, the phone number.. whatever this may be)

I've spent quite a bit of time on this and it's frustrating, i've though of using lambda expressions, but not 100% sure on wether this would actually work in this case.

Thanks for any help you can provide.

解决方案

To the people that commented, thank you, sorry for things being a little.. less detailed when it comes to code portions, I wasn't able to show this code as i don't have anything attempting the retrieve items as i kept failing at doing so.. HOWEVER i figured this out this morning after i dropped my partner off to work.

This is my code solution:

private async void btnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                sortedItems = new List<favouritesitems>();
                for (int i = 0; i < lvFavourites.Items.Count(); i++)
                {
                    lvFavourites.SelectedIndex = i;
                    FavouritesItems item = (FavouritesItems)lvFavourites.SelectedItem;
                    sortedItems.Add(new FavouritesItems(item));
                }
                await SaveObjectToXml<list><favouritesitems>>(sortedItems,  "favouritelistItems.xml");
                MessageDialog msg = new MessageDialog("Favourites have been successfully saved to file");
                await msg.ShowAsync();
            }
            catch (Exception ex)
            {
                MessageDialog msg = new MessageDialog("Unknown Error has occured: " + ex.ToString());
                await msg.ShowAsync();
            }
        }</favouritesitems></list></favouritesitems>



What i had to do was add another constructor to my FavouritesItems Class (which is the object that is being added to the ListView.Items Item Collection)

This is my FavouritesItems Class:

public class FavouritesItems
    {
        private FavouritesItems selectedItem;

        public string Section { get; set; }
        public string Destination { get; set; }

        public FavouritesItems(string section, string destination)
        {
            this.Section = section;
            this.Destination = destination;
        }

        public FavouritesItems()
        {
        }

        public FavouritesItems(FavouritesItems selectedItem)
        {
            this.Section = selectedItem.Section;
            this.Destination = selectedItem.Destination;
        }
    }



Using this new constructor i was able to grab a COMPLETED item (both the section and destination are added seperately as strings, i.e ListView.Items.Add(new FavouritesItems("Sight-Seeing", "Melbourne"); etc) and was able to retrieve the attribute values specifically and seperately.

Considering all this was in a FOR loop, i was able to do this for each item in the list.. one at a time. By ensuring that the selectedIndex was equal to the FOR loop integer i.

Hopefully this helps anyone else that might have a similiar issue to mine when it comes to Windows Store Apps and using ListView's and their ItemCollections.

Note: i know the first code segment is kind of.. well oddly formatted.. not sure how to fix it. (posted code from visual studio into the tags and it ended up like it looks)


这篇关于如何访问ListView的Items值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆