Share via


Dragenter not firing.

Question

Monday, October 23, 2017 5:40 PM

I cant get dragenter to fire.

The dragdrop routine works if I drag text from another app.

I am trying to drag data from a treeview into form1.

The treeview is inside a grid.

            this.AllowDrop = true;

            
            tbar2.DragEnter += new System.Windows.DragEventHandler(treeView1_DragEnter);
            tbar2.DragOver +=  new System.Windows.DragEventHandler(treeView1_DragOver);

            this.Drop += new System.Windows.DragEventHandler(treeView1_DragDrop);

            








        // Set the target drop effect to the effect 
        // specified in the ItemDrag event handler.
        private void treeView1_DragEnter(object sender, System.Windows.DragEventArgs e)
        {
           

            e.Effects = System.Windows.DragDropEffects.Copy;

            //            if (e.Data.GetDataPresent(System.Windows.DataFormats.Text))
            //              e.Effects = System.Windows.DragDropEffects.Copy;


          

            System.Windows.MessageBox.Show("11111111111111111111111111111111");
        }


        // Set the target drop effect to the effect 
        // specified in the ItemDrag event handler.
        private void treeView1_DragOver(object sender, System.Windows.DragEventArgs e)
        {
            e.Effects = System.Windows.DragDropEffects.Copy;

            

            System.Windows.MessageBox.Show("222222222222222222222222222222222");
        }




        private void treeView1_DragDrop(object sender, System.Windows.DragEventArgs e)
        {
            // Retrieve the node that was dragged.
            
           
            String tr;
            tr = e.Data.ToString();
            System.Windows.MessageBox.Show(tr);

            System.Windows.MessageBox.Show("33333333333333333333333333333333333");
        }


        

n.Wright

All replies (5)

Monday, October 23, 2017 11:14 PM ✅Answered

I had to add the enclosed code to get it to work.

I also have a C++ versionof this program and that didn't need the extra code.

        private void ListBox_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {


            object data = GetDataFromListBox(tbar2, e.GetPosition(tbar2));

            if (data != null)
            {
                DragDrop.DoDragDrop(tbar2, data, System.Windows.DragDropEffects.Move);
            }
        }



        private static object GetDataFromListBox(System.Windows.Controls.TreeView source, Point point)
        {
            UIElement element = source.InputHitTest(point) as UIElement;
            if (element != null)
            {
                object data = DependencyProperty.UnsetValue;
                while (data == DependencyProperty.UnsetValue)
                {
                    data = source.ItemContainerGenerator.ItemFromContainer(element);

                    if (data == DependencyProperty.UnsetValue)
                    {
                        element = VisualTreeHelper.GetParent(element) as UIElement;
                    }

                    if (element == source)
                    {
                        return null;
                    }
                }

                if (data != DependencyProperty.UnsetValue)
                {
                    return data;
                }
            }

            return null;
        }

n.Wright


Tuesday, October 24, 2017 3:12 AM ✅Answered

It is a C# WPF program.

It is using a grid for the user interface.

The problem has been fixed as above.

I think the C# drag and drop doesn't have the itemdrag like C++.

So I had to add a mouse preview event handler.

Once this was added the drag and drop started working.

n.Wright


Monday, October 23, 2017 6:59 PM

I also have a textbox in the same grid and that does allow drag drop.

n.Wright


Monday, October 23, 2017 7:10 PM

I also have a textbox in the same grid and that does allow drag drop.

n.Wright

Listbox and listview have the same problem.

n.Wright


Tuesday, October 24, 2017 3:09 AM

Hello nigelwright7557,

What is the type of your project? Winform,Uwp,Wpf or others, please provide more details about your program  so that we can do further test with it.

Sincerely,

Neil Hu

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].