Drop on TRASH BIN

Mar 21, 2009 at 6:48 PM
I needed the ability to drop a dragsource onto a trash bin drag target.  My code (VB) I verify that the user does want to delete it.  If so, it is deleted from the database and the dragsource is removed from the trash bin "panel".  If the user cancels the delete, the dropsource needs to return to the original position.  I have made the following changes to your code and it all is working as planned.  I don't know if this is the best way of doing this or not.  Let me know.

Changes:

1)  I made the ReturnToOriginalPosition public, so I can access it from my code.  This actually seems like it might be useful in other scenarios as well.

2) I added a public bool called DeleteDropSource to the DropTarget to trigger this effect.

3) At line 328 of the DropTarget I check this boolean.  If it is set, I simply ignore redisplaying it in the new drop target.

_____________________________________________
DragSource.cs

public void ReturnToOriginalPosition()

DropTarget.cs
 /// <summary>
        /// Do not redraw the Dropsource on this DropTarget
        /// (As in dropping on to a trash can)
        /// </summary>
        public bool DeleteDropSource { get; set; }

LINES 328 ELSE:
            {
                // If this is a trash bin, do not redisplay the dragsource
                // Move the dragsouce back to it's original position
                if (DeleteDropSource != true)
                {
                    // remove from current parent
                    // reset position of dragsource, so control is on top of ghost, right
                    // before actually moving it.
                    args.DragSource.ResetMyPosition();

                    // remove from current parent
                    ((Panel)VisualTreeHelper.GetParent(args.DragSource)).Children.Remove(args.DragSource);

                    MainContentControl.Children.Clear();
                    MainContentControl.Children.Add(args.DragSource);
                }

In my code (visual basic):
            If MessageBox.Show("Are you sure you want to delete """ & box.Text & """?", "Confirm Delete",    MessageBoxButton.OKCancel) = MessageBoxResult.OK Then
  '''
  ''' CODE TO REMOVE ITEM FROM DATABASE
  '''

                DirectCast(VisualTreeHelper.GetParent(drag), Panel).Children.Remove(drag)
            Else
                drag.ReturnToOriginalPosition()
            End If



DragSource.cs
public void ReturnToOriginalPosition()

DropTarget.cs
 /// <summary>
        /// Do not redraw the Dropsource on this DropTarget
        /// (As in dropping on to a trash can)
        /// </summary>
        public bool DeleteDropSource { get; set; }

LINES 328 ELSE:
            {
                // If this is a trash bin, do not redisplay the dragsource
                // Move the dragsouce back to it's original position
                if (DeleteDropSource != true)
                {
                    // remove from current parent
                    // reset position of dragsource, so control is on top of ghost, right
                    // before actually moving it.
                    args.DragSource.ResetMyPosition();

                    // remove from current parent
                    ((Panel)VisualTreeHelper.GetParent(args.DragSource)).Children.Remove(args.DragSource);

                    MainContentControl.Children.Clear();
                    MainContentControl.Children.Add(args.DragSource);
                }

In my code (visual basic):
            If MessageBox.Show("Are you sure you want to delete """ & box.Text & """?", "Confirm Delete",    MessageBoxButton.OKCancel) = MessageBoxResult.OK Then
  '''
  ''' CODE TO REMOVE ITEM FROM DATABASE
  '''

                DirectCast(VisualTreeHelper.GetParent(drag), Panel).Children.Remove(drag)
            Else
                drag.ReturnToOriginalPosition()
            End If