Search code examples
wpfdrag-and-dropadorner

WPF - Drag-drop - Adorner disappers outside the control


I'm using WPF to create two ListViews and implement drag-drop functionality. (both intra-listview and inter-listview)

I found an interesting post here which does that.

However, there is a problem. When I drag a listviewitem from listView1, I see the adorner (the ghost image) only inside listView1. When I want to drop the listviewItem on ListView2, I must see the adorner there as well. Basically, the adorner appears only on the listView from which the drag operation started. Once it is outside the listView, it disappears.

I have done a bit of research and could not find a way to make the adorner visible outside the control from which the drag was initiated.

Can anyone help me with some suggestions?


Solution

  • Wire up the GiveFeedback event to update the adorner location beyond the listview. Updated ListView property from the example and method below (and in the listview_DragLeave method you won't want to collapse the adorner):

        /// <summary>
        /// Gets/sets the ListView whose dragging is managed.  This property
        /// can be set to null, to prevent drag management from occuring.  If
        /// the ListView's AllowDrop property is false, it will be set to true.
        /// </summary>
        public ListView ListView
        {
            get { return listView; }
            set
            {
                if( this.IsDragInProgress )
                    throw new InvalidOperationException( "Cannot set the ListView property during a drag operation." );
    
                if( this.listView != null )
                {
                    #region Unhook Events
    
                    this.listView.PreviewMouseLeftButtonDown -= listView_PreviewMouseLeftButtonDown;
                    this.listView.PreviewMouseMove -= listView_PreviewMouseMove;
                    this.listView.DragOver -= listView_DragOver;
                    this.listView.DragLeave -= listView_DragLeave;
                    this.listView.DragEnter -= listView_DragEnter;
                    this.listView.GiveFeedback -= listView_GiveFeedback;
                    this.listView.Drop -= listView_Drop;
    
                    #endregion // Unhook Events
                }
    
                this.listView = value;
    
                if( this.listView != null )
                {
                    if( !this.listView.AllowDrop )
                        this.listView.AllowDrop = true;
    
                    #region Hook Events
    
                    this.listView.PreviewMouseLeftButtonDown += listView_PreviewMouseLeftButtonDown;
                    this.listView.PreviewMouseMove += listView_PreviewMouseMove;
                    this.listView.DragOver += listView_DragOver;
                    this.listView.DragLeave += listView_DragLeave;
                    this.listView.DragEnter += listView_DragEnter;
                    this.listView.GiveFeedback += listView_GiveFeedback;
                    this.listView.Drop += listView_Drop;
    
                    #endregion // Hook Events
                }
            }
        }
    
        void listView_GiveFeedback(object sender, GiveFeedbackEventArgs e)
        {
            if (this.ShowDragAdornerResolved)
                this.UpdateDragAdornerLocation();
        }