簡體   English   中英

在winform中選擇ListView項目

[英]ListView Item select in winform

我想在單擊時選擇 ListView 中的項目。 我也想知道我點擊了什么。 我使用 c# 處理 winforms。我還想知道如何單擊所有行?

只需處理列表上的Click事件並使用ListView.SelectedItems屬性來獲取選擇的項目:

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

此外,如果您將 xaml 用於窗口,則必須將 MouseUp="listView1_Click" 屬性添加到 ListView 標記

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM