【发布时间】:2009-11-16 16:04:34
【问题描述】:
我采用了以下模式将 ListViewItems 放入具有多列的 ListView 中(当我想显示有关 MyObject 类型列表的信息时),我只是好奇看看这是否是完成这项任务的最佳方式,或者代码中是否有更高效和可读的方法:
- 创建一个继承的
ListViewItem类,该类在构造函数中采用MyObject对象 - 我将称之为MyObjectListViewItem- 和一个用于清除和重新填充 ListViewItem 子项的Refresh()方法。 - 用我的新 MyObjectListViewItem 项填充 ListView。
示例:
public MyObject MyObject { get; set; }
public MyObjectListViewItem(MyObject myObj)
{
MyObject = myObj;
this.Refresh();
}
public void Refresh()
{
this.SubItems.Clear();
this.Text = myObj.FirstColumnProperty;
this.SubItems.Add(myObj.SecondColumnProperty); // etc...
}
建议?更好的方法?
【问题讨论】:
标签: c# winforms listview listviewitem