简体   繁体   English

在winform中选择ListView项目

[英]ListView Item select in winform

I want to select item in a ListView upon clicking.我想在单击时选择 ListView 中的项目。 I also want to know what I clicked.我也想知道我点击了什么。 I work on winforms with c#.I also want to know How I can clicking the all row?我使用 c# 处理 winforms。我还想知道如何单击所有行?

Just handle the Click event on the list and use the ListView.SelectedItems property to get what items are selected:只需处理列表上的Click事件并使用ListView.SelectedItems属性来获取选择的项目:

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

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

u can use MouseEventArgs and get the mouse location check if it exists inside the selected item bound , that means the click was made on the selected item .您可以使用 MouseEventArgs 并获取鼠标位置检查它是否存在于所选项目绑定内,这意味着在所选项目上进行了单击。

EDIT : example :编辑:示例:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM