代码之家  ›  专栏  ›  技术社区  ›  Simon

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

  •  4
  • Simon  · 技术社区  · 16 年前

    我正在显示一组搜索结果 ListView . 第一列包含搜索项,第二列显示匹配项的数目。

    有上万行,所以 列表视图 处于虚拟模式。

    我想更改此项,以便第二列将匹配项显示为超链接,其方式与 LinkLabel 显示链接;当用户单击链接时,我希望收到一个事件,该事件将允许我在应用程序的其他位置打开匹配项。

    这有可能吗?如果有,怎么办?

    编辑:我想我说得不够清楚-我想 倍数 一列中的超链接 倍数 单个超链接 链接标签

    5 回复  |  直到 15 年前
        1
  •  8
  •   Hans Passant    16 年前

    你可以很容易地伪造它。确保添加的列表视图项具有useitemstyleforsubitems=false,以便可以将子项的前景色设置为蓝色。实现mousemove事件,这样您就可以在“link”下面加下划线并更改光标。例如:

    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;
      }
    }
    

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

        2
  •  3
  •   Community Mohan Dere    9 年前

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

    alt text

    This recipe 记录(非常简单的)过程以及如何定制它。

        3
  •  1
  •   Codesleuth    16 年前

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

    使用此控件,可以在 ListView ,但每行有更多定制。

        4
  •  1
  •   Patrik Svensson Martin Ender    16 年前

    你可以通过继承 ListView 控件重写方法 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);
        }
    }
    
        5
  •  0
  •   Giorgi    16 年前

    你可以设定 HotTracking 设置为true,以便当用户将鼠标悬停在项上时,该项显示为链接。