【问题标题】:ListView Item select in winformWinform中的ListView项目选择
【发布时间】:2011-08-28 19:08:45
【问题描述】:

我想在单击时选择 ListView 中的项目。我也想知道我点击了什么。 我使用 c# 处理 winforms。我也想知道如何单击所有行?

【问题讨论】:

    标签: c# .net winforms listview


    【解决方案1】:

    只需处理列表中的Click 事件,并使用ListView.SelectedItems 属性来获取哪些项目被选中:

    private void listView1_Click(object sender, EventArgs e)
    {
        var firstSelectedItem = listView1.SelectedItems[0];
    }
    

    【讨论】:

    • 如果我在同一个项目上单击两次,它会在不应该发生的情况下分派两次事件,因为选择没有改变。
    【解决方案2】:

    如果您使用 xaml 作为窗口,则必须将 MouseUp="listView1_Click" 属性添加到 ListView 标记

    【讨论】:

      【解决方案3】:

      你可以使用 MouseEventArgs 并获取鼠标位置检查它是否存在于选定的项目绑定内,这意味着点击是在选定的项目上进行的。

      编辑: 示例:

          private void myList_MouseDoubleClick(object sender, MouseEventArgs e)
          {
              if (myList.SelectedItems.Count  >= 1)
              {
                  ListViewItem item = myList.SelectedItems[0];
      
                  //here i check for the Mouse pointer location on click if its contained 
                  // in the actual selected item's bounds or not .
                  // cuz i ran into a problem with the ui once because of that ..
                  if (item.Bounds.Contains(e.Location))
                  {
                      MessageBox.Show("Double Clicked on :"+item.Text);
                  }
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-19
        相关资源
        最近更新 更多