我可以在ListView的详细模式下显示链接吗?

我在ListView显示一组搜索结果。 第一列包含搜索词,第二列显示匹配数。

有数万行,因此ListView处于虚拟模式。

我想改变它,以便第二列显示匹配作为超链接,就像LinkLabel显示链接一样; 当用户点击链接时,我希望收到一个事件,让我在我们的应用程序的其他地方打开匹配。

这是可能的,如果是的话,怎么样?

编辑:我认为我不够清楚 – 我想在一个列中有多个超链接,就像在一个LinkLabel可以有多个超链接一样。

你可以轻易伪造它。 确保您添加的列表视图项具有UseItemStyleForSubItems = false,以便您可以将子项的ForeColor设置为蓝色。 实现MouseMove事件,以便为“链接”加下划线并更改光标。 例如:

 ListViewItem.ListViewSubItem mSelected; private void listView1_MouseMove(object sender, MouseEventArgs e) { var info = listView1.HitTest(e.Location); if (info.SubItem == mSelected) return; if (mSelected != null) mSelected.Font = listView1.Font; mSelected = null; listView1.Cursor = Cursors.Default; if (info.SubItem != null && info.Item.SubItems[1] == info.SubItem) { info.SubItem.Font = new Font(info.SubItem.Font, FontStyle.Underline); listView1.Cursor = Cursors.Hand; mSelected = info.SubItem; } } 

请注意,此代码段会检查第二列是否hover,并根据需要进行调整。

使用ObjectListView – 围绕标准ListView的开源包装器。 它直接支持链接:

替代文字

这个食谱记录了(非常简单的)过程以及如何定制它。

这里的其他答案很棒,但如果您不想一起破解某些代码,请查看支持LinkLabel等效列的DataGridView控件。

使用此控件,您可以在ListView中获得详细信息视图的所有function,但每行可以进行更多自定义。

您可以通过inheritanceListView控件来覆盖OnDrawSubItem方法。
这是一个非常简单的例子,说明你可以做什么:

 public class MyListView : ListView { private Brush m_brush; private Pen m_pen; public MyListView() { this.OwnerDraw = true; m_brush = new SolidBrush(Color.Blue); m_pen = new Pen(m_brush) } protected override void OnDrawColumnHeader(DrawListViewColumnHeaderEventArgs e) { e.DrawDefault = true; } protected override void OnDrawSubItem(DrawListViewSubItemEventArgs e) { if (e.ColumnIndex != 1) { e.DrawDefault = true; return; } // Draw the item's background. e.DrawBackground(); var textSize = e.Graphics.MeasureString(e.SubItem.Text, e.SubItem.Font); var textY = e.Bounds.Y + ((e.Bounds.Height - textSize.Height) / 2); int textX = e.SubItem.Bounds.Location.X; var lineY = textY + textSize.Height; // Do the drawing of the underlined text. e.Graphics.DrawString(e.SubItem.Text, e.SubItem.Font, m_brush, textX, textY); e.Graphics.DrawLine(m_pen, textX, lineY, textX + textSize.Width, lineY); } } 

您可以将HotTracking设置为true,以便当用户将鼠标hover在项目上时,它将显示为链接。