【问题标题】:Select subitem in Listview and change value在 Listview 中选择子项并更改值
【发布时间】:2014-02-23 00:49:02
【问题描述】:

我有一个处于“详细信息”模式的列表视图,看起来像:

#################
Name  #  Property
#################
#Itm1 # Subitm1
#Itm2 # Subitm2
#################

非常简单,但我遇到的问题是我无法在运行时在列表中选择“Subitm1”。我可以选择并突出显示第一列中的每个项目,但是单击第二列中的任何项目都不会执行任何操作(我希望它会像第一列中那样突出显示该项目)。

具体来说,我正在尝试为用户添加能够双击子项并直接在列表视图中更改其值的功能。这里有没有我遗漏的特定设置?

【问题讨论】:

  • 这是什么平台,wpf,winform,还是?
  • 虽然您可以使用 ListView 控件执行此操作,但设置起来很痛苦。您是否考虑过使用 Grid 控件?
  • 我有,但我个人觉得网格视图很讨厌。

标签: c# .net winforms listview subitem


【解决方案1】:

如果要在单击子项时选择整行,请尝试使用ListViewFullRowSelect 属性。 要处理双击子项,试试这个:

private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
{
    ListViewHitTestInfo hit = listView1.HitTest(e.Location);
    // Use hit.Item
    // Use hit.SubItem
}

如果你想让最终用户在列表视图中编辑子项的文本,恐怕最简单的方法是使用网格控件。另一种方法是尝试这样的事情:

private readonly TextBox txt = new TextBox { BorderStyle = BorderStyle.FixedSingle, Visible = false };

public Form1()
{
    InitializeComponent();
    listView1.Controls.Add(txt);
    listView1.FullRowSelect = true;
    txt.Leave += (o, e) => txt.Visible = false;
}

private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
{
    ListViewHitTestInfo hit = listView1.HitTest(e.Location);

    Rectangle rowBounds = hit.SubItem.Bounds;
    Rectangle labelBounds = hit.Item.GetBounds(ItemBoundsPortion.Label);
    int leftMargin = labelBounds.Left - 1;
    txt.Bounds = new Rectangle(rowBounds.Left + leftMargin, rowBounds.Top, rowBounds.Width - leftMargin - 1, rowBounds.Height);
    txt.Text = hit.SubItem.Text;
    txt.SelectAll();
    txt.Visible = true;
    txt.Focus();
}

【讨论】:

  • 嗨,有没有办法确定正在编辑的项目属于哪一列?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-20
  • 2011-11-18
  • 1970-01-01
  • 1970-01-01
  • 2023-03-09
相关资源
最近更新 更多