编辑ListView控件组头就像我们可以编辑ListViewItems [英] Edit ListView Group Headers like we can edit ListViewItems

查看:178
本文介绍了编辑ListView控件组头就像我们可以编辑ListViewItems的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个WinForms应用程序,我们可以通过点击他们两次重新命名的ListView项目。我们能以某种方式重命名组头以同样的方式?有没有一种方法,使这个?

In a WinForms app, we can re-name ListView Items by clicking them twice. Can we somehow rename Group Headers the same way? Is there a way to enable this?

推荐答案

我想这毕竟是可行的,尽管不是简单地使一个属性。

I guess it is doable after all, albeit not by simply enabling a property..

注:code以下假设的ListView 详细信息模式

Note: The code below assumes that the ListView is in Details mode!

诀窍告诉集团从EMPTY的空间来测试右侧的的ListView 的。另一个技巧是等待一段:在点击将选择组项目。只有这样,我们才能继续进行。

The trick to tell a Group from emtpy space is to test the right side of the ListView. Another trick is to wait a little: The click will select the Group Items. Only after that can we proceed..

下面是覆盖一个例子集团文本框

Here is an example that overlays the Group with a TextBox:

// class variable to test if have been hit twice in a row
ListViewGroup lastHitGroup = null;

private void listView1_MouseDown(object sender, MouseEventArgs e)
{
    // check left side to see if we are at the empty space
    ListViewItem lvi = listView1.GetItemAt(4, e.Y);
    // yes, no action! reset  group
    if (lvi != null)  { lastHitGroup = null; return; }
    // get the height of an Item
    int ih = listView1.GetItemRect(0).Height;
    // to get the group we need to check the next item:
    ListViewItem lviNext = listView1.GetItemAt(4, e.Y + ih);
    // no next item, maybe the group is emtpy, no action
    if (lviNext == null) return;
    // this is our group
    ListViewGroup editedGroup = lviNext.Group;
    // is this the 2nd time?
    if (lastHitGroup != editedGroup) {lastHitGroup = editedGroup; return;}
    // we overlay a TextBox
    TextBox tb = new TextBox();
    tb.Parent = listView1;
    // set width as you like!
    tb.Height = ih;
    // we position it over the group header and show it
    tb.Location = new Point(0, lviNext.Position.Y - ih - 4);
    tb.Show();
    // we need two events to quit editing
    tb.KeyPress += (ss, ee) =>
    {
        if (ee.KeyChar == (char)13)  // success
        {
            if (editedGroup != null && tb.Text.Length > 0)
                editedGroup.Header = tb.Text;
            tb.Hide();
            ee.Handled = true;
        }
        else if (ee.KeyChar == (char)27)  // abort
        {
            tb.Text = ""; tb.Hide(); ee.Handled = true;
        }

    };
    tb.LostFocus += (ss, ee) =>  // more success
    {
       if (editedGroup != null && tb.Text.Length > 0)
          editedGroup.Header = tb.Text;
       tb.Hide();
    };
    // we need to wait a little until the group items have been selected
    Timer lvTimer = new Timer();
    lvTimer.Interval = 333;  // could take longer for a huge number of items!
    lvTimer.Tick += (ss,ee) => { tb.Focus(); lvTimer.Stop();};
    lvTimer.Start();

}

这篇关于编辑ListView控件组头就像我们可以编辑ListViewItems的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆