在详细列表视图中管理 ListViewItems 的最佳方法?
我采用了以下模式将 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...
}
建议?更好的方法?
I've adopted the following pattern for putting ListViewItems in a ListView with multiple columns (when I want to display information about a list of MyObject
types), and I'm just curious to see if this is the best way to accomplish this task, or if there's anything more efficient and readable in code:
- Create an inherited
ListViewItem
class that takes aMyObject
object in the constructor - I'll call thisMyObjectListViewItem
- and aRefresh()
method that clears and re-populates the ListViewItem subitems. - Populate the ListView with my new MyObjectListViewItem items.
example:
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...
}
Suggestions? Better ways?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否考虑过使用 BindingSource,或者创建自己的实现 IBindingListView?这使得对数据及其状态的关注仅限于数据本身,而不是任何使用它的控件。由于 .NET 控件已构建为可与 BindingSource 配合使用,因此您可以利用一些更强大的功能。该控件不显式调用屏幕刷新,而是仅负责响应绑定源引发的事件,以及通知该控件是否已准备好刷新而不强制刷新的控制器。
Have you considered using a BindingSource, or creating your own which implements IBindingListView? This keeps concerns about the data and its state scoped to the data itself and not on any controls which consume it. Since .NET controls are already built to work with BindingSources, you can take advantage of some more robust functionality. Instead of explicitly invoking a screen refresh, the control is simply responsible for responding to events raised by the binding source, and a controller that notifies whether the control is ready to be refreshed without forcing it.
制作知道如何构建自身的 ListViewItems 是一个好主意。
如果你稍微扩展一下这个想法,你就可以让列知道如何构建每个子项,这也使它们能够自动对 ListView 进行排序,支持分组以及复制/拖放行。这只是 ObjectListView 为您做的一些事情。
ObjectListView 是围绕 .NET WinForms ListView 控件的开源包装器,它使 ListView 更易于使用,并且添加了一些非常好的新功能并解决了一些恼人的错误/限制。
如果您确实喜欢 @Rex 使用
BindingSource
的想法,ObjectListView 项目还提供了一个可绑定数据的数据感知DataListView
。Making ListViewItems that know how to build themselves is a good idea.
If you extend that idea a little, you make the columns know how to build each subitem, which also allows them to be able to automatically sort the ListView, support grouping and copy/drag and drop rows. This is just a few of the things that ObjectListView does for you.
ObjectListView is an open source wrapper around a .NET WinForms ListView control that makes the ListView much easier to use -- as well as adding some very nice new features and getting around some annoying bugs/limitations.
If you did like @Rex's idea of using a
BindingSource
, the ObjectListView project also provides a data-awareDataListView
which is data bindable.