在wpf中编辑列表 [英] edit list in wpf
本文介绍了在wpf中编辑列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨..
我在wpf中使用List< Type>(Type是一个类对象,其中包含id,name,price,quantity等字段).我想编辑具有选定ID的列表中的一行.
我该怎么办?
谢谢.
Hi..
i am using List<Type>(Type is a class object which contains fields like id,name,price,quantity) in wpf. i want to edit a row in a list with selected id.
how can i do this?
thanks.
推荐答案
您的问题是很讨厌的单词.您是否要允许用户编辑列表视图中的行,还是要更改List<>
中的值?
Your question is porrly word. Do you want to allow the user to edit the row in a listview, or do you want to change the value in the List<>
?
在列表中查找具有所需ID的项目并更改该项目的所需属性.您可以通过两种方法找到该项目-使用for循环遍历列表,或使用Linq.就个人而言,我认为for循环会更有效,但是Linq语句所需的代码更少.
Find the item in the list with the desired ID and change the desired property for that item. There are a couple of ways you can find the item - iterating the list with a for loop, or using Linq. Personally, I think a for loop is more efficient, but a Linq statement requires less code.
for (int i = 0; i < myList.Count; i++)
{
if (myList[i].ID == ID)
{
myList[i].myPropery = newValue;
break;
}
}
或使用linq
or with linq
var result = (from MyItem item in myList
where item.ID = ID
select item);
result.myProperty = property;
上面提供的代码可能需要调整,因为我是从内存中完成的.顺便说一句,这都是非常基础的编程内容.也许您应该更愿意使用Google.
Code provided above may need tweaking because I did it from memory. BTW, this is all very basic programming stuff. Maybe you should be more open to using google.
这篇关于在wpf中编辑列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文