如何为 ListViewSubItem 设置图标?

发布于 2024-08-13 21:58:00 字数 84 浏览 2 评论 0原文

在 ListView 中,每个项目都可以有图标。
在详细信息模式下查看时,该图标显示在最左侧的列中。

我可以在其他列中显示图标吗?

In a ListView you can have icons on each item.
When viewing in Details-mode, the icon is shown in the left-most column.

Can I show an icon in some other column?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

咿呀咿呀哟 2024-08-20 21:58:00

ListView 控件本身不支持子项中的图像。最简单的方法是切换到 DataGridView 并使用 DataGridViewImageColumn。如果这是不可能的,那么您需要使用 ListView 控件中的自定义绘制支持自行绘制图标。为此,请设置 ListView.OwnerDraw = true 并处理 ListView.DrawSubItemListView.DrawColumnHeader 事件。

private void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
{
    // Only interested in 2nd column.
    if (e.Header != this.columnHeader2)
    {
        e.DrawDefault = true;
        return;
    }

    e.DrawBackground();
    var imageRect = new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Height, e.Bounds.Height);
    e.Graphics.DrawImage(SystemIcons.Information.ToBitmap(), imageRect);
}

private void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
{
    e.DrawDefault = true;
}

The ListView control does not support images in sub-items natively. The easiest thing to do is switch to a DataGridView and use a DataGridViewImageColumn. If that is not possible, then you'll need to draw the icons yourself using the custom draw support in the ListView control. To do this set ListView.OwnerDraw = true and handle the ListView.DrawSubItem and ListView.DrawColumnHeader events.

private void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
{
    // Only interested in 2nd column.
    if (e.Header != this.columnHeader2)
    {
        e.DrawDefault = true;
        return;
    }

    e.DrawBackground();
    var imageRect = new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Height, e.Bounds.Height);
    e.Graphics.DrawImage(SystemIcons.Information.ToBitmap(), imageRect);
}

private void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
{
    e.DrawDefault = true;
}
最单纯的乌龟 2024-08-20 21:58:00

使用 P/Invoke 并向listview(您应该在创建控件时设置LVS_EX_SUBITEMIMAGES样式或通过LVM_SETEXTENDEDLISTVIEWSTYLE),指定子项索引和相应的图像索引。您需要对插入的每个列表项执行此操作。

Use P/Invoke and send LVM_SETITEM message to the listview (you should set LVS_EX_SUBITEMIMAGES style on control creation or via LVM_SETEXTENDEDLISTVIEWSTYLE), specify the subitem index and the corresponding image index. You will need to do it for every list item you insert.

把时间冻结 2024-08-20 21:58:00

ObjectListView 是 .NET Winforms ListView 的开源包装器。它使用 @ligget78 提到的 p/invoke 策略支持子项上的图像。它还解决了 ListView 的许多其他常见问题。

它允许您以最小的努力制作非常漂亮的 ListView:

替代文字
(来源:sourceforge.net

ObjectListView is an open source wrapper around a .NET Winforms ListView. It supports images on subitems using the p/invoke strategy that that @ligget78 mentioned. It also solves many other common problems with a ListView.

It allows you to make very nice looking ListViews with a minimum effort:

alt text
(source: sourceforge.net)

万劫不复 2024-08-20 21:58:00

继承ListView,绘制自己的图标。

public class MyListView : ListView
{
    protected override void OnDrawSubItem(System.Windows.Forms.DrawListViewSubItemEventArgs e)
    {
        base.OnDrawSubItem(e);
    }
}

Inherit from ListView and draw your own icons.

public class MyListView : ListView
{
    protected override void OnDrawSubItem(System.Windows.Forms.DrawListViewSubItemEventArgs e)
    {
        base.OnDrawSubItem(e);
    }
}
○愚か者の日 2024-08-20 21:58:00

图标显示在“第一”列中,这也是键盘前缀搜索的基础。一种可能的解决方案是通过将第一列的 DisplayIndex 设置为其他值来重新排序列。

listView1.Columns[0].DisplayIndex = 1;

当然,只有当您只需要一列中的图标时,这才有效。

The icon is shown in the "first" column, and this is also the basis for the keyboard prefix search. One possible solution could be to reorder the columns by setting the DisplayIndex of the first column to something else.

listView1.Columns[0].DisplayIndex = 1;

This of course only works if you need an icon in only one column.

难如初 2024-08-20 21:58:00

.NET 对此没有支持。

看看这个项目。

There's no .NET support for this.

Have a look at this project.

旧话新听 2024-08-20 21:58:00

抓住这个:

http://social.msdn.microsoft.com/forums/en-US/winforms/thread/d25b4ffa-2ea4-43cd-a3ae-8dd0387197ae/

除了接受的答案之外,您还应该将 DrawItem 事件处理为好吧,否则行不通。

Take a loot at this:

http://social.msdn.microsoft.com/forums/en-US/winforms/thread/d25b4ffa-2ea4-43cd-a3ae-8dd0387197ae/

In addition to the accepted answer, you should handle the DrawItem event as well, or it will not work.

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