Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I show the item that is being dragged in WPF?

I have been working on a WPF Application that is essentially a WYSIWYG editor, and is using drag and drop functionality. I have the drag and drop functionality working, but need to make it more intuitive and user friendly. Part of this will involve actually showing the item being dragged. What is the easiest way to do this? The items I am dragging are nothing really special,but I am not even sure where to look for how to do this.

like image 473
TheJediCowboy Avatar asked Feb 02 '11 17:02

TheJediCowboy


1 Answers

You will need to make use of DragDrop.GiveFeedback amongst other things.

Trivial example from an older blog post in dealing with cursor manipulation...

        private void StartDragCustomCursor(MouseEventArgs e)
        {

            GiveFeedbackEventHandler handler = new GiveFeedbackEventHandler(DragSource_GiveFeedback);
            this.DragSource.GiveFeedback += handler; 
            IsDragging = true;
            DataObject data = new DataObject(System.Windows.DataFormats.Text.ToString(), "abcd");
            DragDropEffects de = DragDrop.DoDragDrop(this.DragSource, data, DragDropEffects.Move);
            this.DragSource.GiveFeedback -= handler; 
            IsDragging = false;
        }

        void DragSource_GiveFeedback(object sender, GiveFeedbackEventArgs e)
        {
                try
                {
                    //This loads the cursor from a stream .. 
                    if (_allOpsCursor == null)
                    {
                        using (Stream cursorStream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(
            "SimplestDragDrop.DDIcon.cur"))
                        {
                            _allOpsCursor = new Cursor(cursorStream);
                        } 
                    }
                    Mouse.SetCursor(_allOpsCursor);

                    e.UseDefaultCursors = false;
                    e.Handled = true;
                }
                finally { }
        }
like image 77
Aaron McIver Avatar answered Oct 13 '22 00:10

Aaron McIver