在详细列表视图中管理 ListViewItems 的最佳方法?

发布于 2024-08-11 06:54:40 字数 729 浏览 7 评论 0原文

我采用了以下模式将 ListViewItems 放入具有多列的 ListView (当我想显示有关 MyObject 类型列表的信息时),我只是很想知道这是否是完成此任务的最佳方法,或者代码中是否有更高效和可读的内容:

  1. 创建一个继承的 ListViewItem 类,该类采用 MyObject 对象构造函数 - 我将其称为 MyObjectListViewItem - 以及清除并重新填充 ListViewItem 子项的 Refresh() 方法。
  2. 使用我的新 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:

  1. Create an inherited ListViewItem class that takes a MyObject object in the constructor - I'll call this MyObjectListViewItem - and a Refresh() method that clears and re-populates the ListViewItem subitems.
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

单身情人 2024-08-18 06:54:40

您是否考虑过使用 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.

你爱我像她 2024-08-18 06:54:40

制作知道如何构建自身的 ListViewItems 是一个好主意。

如果你稍微扩展一下这个想法,你就可以让列知道如何构建每个子项,这也使它们能够自动对 ListView 进行排序,支持分组以及复制/拖放行。这只是 ObjectListView 为您做的一些事情。

ObjectListView 是围绕 .NET WinForms ListView 控件的开源包装器,它使 ListView 更易于使用,并且添加了一些非常好的新功能并解决了一些恼人的错误/限制。

操作中的简单 ObjectListView

如果您确实喜欢 @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.

Simple ObjectListView in action

If you did like @Rex's idea of using a BindingSource, the ObjectListView project also provides a data-aware DataListView which is data bindable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文