Get the item doubleclick event of listview

后端 未结 16 1329
没有蜡笔的小新
没有蜡笔的小新 2020-12-01 02:17

What do I need to do in order to reference the double click event for a listview control?

相关标签:
16条回答
  • 2020-12-01 02:41
        private void positionsListView_DoubleClick(object sender, EventArgs e)
        {
            if (positionsListView.SelectedItems.Count == 1)
            {
                ListView.SelectedListViewItemCollection items = positionsListView.SelectedItems;
    
                ListViewItem lvItem = items[0];
                string what = lvItem.Text;
    
            }
        }
    
    0 讨论(0)
  • 2020-12-01 02:41

    I needed that as well. I found that on msdn:

    http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.activation.aspx

    I think this delegate is for that.

    0 讨论(0)
  • 2020-12-01 02:42

    The sender is of type ListView not ListViewItem.

        private void listViewTriggers_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            ListView triggerView = sender as ListView;
            if (triggerView != null)
            {
                btnEditTrigger_Click(null, null);
            }
        }
    
    0 讨论(0)
  • 2020-12-01 02:45

    In the ListBox DoubleClick event get the selecteditem(s) member of the listbox, and there you are.

    void ListBox1DoubleClick(object sender, EventArgs e)
        {
            MessageBox.Show(string.Format("SelectedItem:\n{0}",listBox1.SelectedItem.ToString()));
        }
    
    0 讨论(0)
  • 2020-12-01 02:45

    It's annoying, but the best way to do it is something like:

    <DataTemplate Name="MyCoolDataTemplate">
        <Grid Loaded="HookLVIClicked" Tag="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}">
            <!-- your code here -->
        </Grid>
    </DataTemplate>
    

    Then in the code:

    public void HookLVIClicked(object sender, RoutedEventArgs e) {
        var fe = (FrameworkElement)sender;
        var lvi = (ListViewItem)fe.Tag;
        lvi.MouseDoubleClick += MyMouseDoubleClickHandler;
    } 
    
    0 讨论(0)
  • 2020-12-01 02:46

    Use the ListView.HitTest method

        private void listView_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            var senderList  = (ListView) sender;
            var clickedItem = senderList.HitTest(e.Location).Item;
            if (clickedItem != null)
            {
                //do something
            }            
        }
    

    Or the old way

        private void listView_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            var senderList  = (ListView) sender;                        
            if (senderList.SelectedItems.Count == 1 && IsInBound(e.Location, senderList.SelectedItems[0].Bounds))
            {
                //Do something
            }
        }
    
        public  bool IsInBound(Point location, Rectangle bound)
        {
            return (bound.Y <= location.Y && 
                    bound.Y + bound.Height >= location.Y &&
                    bound.X <= location.X && 
                    bound.X + bound.Width >= location.X);
        }
    
    0 讨论(0)
提交回复
热议问题