在winform中选择ListView项目

时间:2011-05-19 11:05:58

标签: c# .net winforms listview

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

3 个答案:

答案 0 :(得分:8)

只需处理列表中的Click事件,然后使用ListView.SelectedItems属性来获取所选项目:

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

答案 1 :(得分:0)

如果您使用xaml for window,那么您必须将MouseUp =“listView1_Click”属性添加到ListView标记

答案 2 :(得分:0)

u可以使用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);
            }
        }
    }