简体   繁体   English

在ListView中更新项目属性

[英]Updating item property in ListView

I am trying to figure out how to update a Object's property in a ListView after render. 我试图弄清楚渲染后如何在ListView中更新Object的属性。 For an example, let's say that the ListView is DataBound to a collection of Employees. 举个例子,假设ListView是DataBound到Employees的集合。 Each row displays the information of the employee. 每行显示员工的信息。 After the table is loaded, I needed to say "If an employee name = [RON] then change it to text [RONALD]". 加载表后,我需要说“如果雇员姓名= [RON],然后将其更改为文本[RONALD]”。

I currently was thinking I could foreach the ListViewDataItems in the ListView, and go from there, but am stuck. 我目前在想我可以在ListView中foreach ListViewDataItems,然后从那里去,但是卡住了。 Any help would be appreciated. 任何帮助,将不胜感激。

foreach(ListViewDataItem entry in lvProjectModeratorEntries.Items)
{
    //I need to find the div where the firsName is 
    //displayed, and run my logic to update it.

}

I also thought I would get it through entry.DataItem but am stuck at that point. 我还认为我可以通过entry.DataItem来获取它,但是在那一刻被卡住了。

It sounds like you could use the INotifyPropertyChanged Interface on your Employee model. 听起来您可以在Employee模型上使用INotifyPropertyChanged接口 In your listview item template, bind the text to the Employee's property that you wish to display. 在您的listview项目模板中,将文本绑定到您希望显示的Employee属性。 Then you can change your Employee objects. 然后,您可以更改Employee对象。

to find each item in a Listview you can loop like this 在Listview中查找每个项目,您可以像这样循环

for (int i = 0; i < lvProjectModeratorEntries.Items.Count; i++)
{
    int ii = 1;
    MessageBox.Show(lvProjectModeratorEntries.Items[i].SubItems[ii].Text);
    ii++;
}

Using your own code: 使用您自己的代码:

foreach (ListViewItem item in lvTest.Items)
{
    if (item.Text == "John")
        item.Text = "John is gone";
}

That should give you a start on how to do things, yet I don't recommend it. 那应该让您开始做事,但是我不建议这样做。 There are more elegant and code-sustainable solutions. 有更多优雅且代码可持续的解决方案。 Have you thought about binding the listview to a List and make all the necessary business logic in the List, instead of the actual view? 您是否考虑过将listview绑定到List并在List中创建所有必要的业务逻辑,而不是实际视图?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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