private void EndDrag(bool processDrop) { // This prevents the mouse up event or the end drag event firing more than once from causing the // events to bubble up and the drop be handled more than once. if (this.HasProcessedEndDrag) { return; } this.HasProcessedEndDrag = true; // Reshow the cursor and hide the source image. this.Host.Cursor = Cursors.Arrow; // Hide the drag cursor. if (this.DragCursor != null) { this.DragCursor.Visibility = Visibility.Collapsed; this.DragCursor.MouseLeftButtonUp -= new MouseButtonEventHandler(OnDragCursorMouseLeftButtonUp); this.DragCursor.MouseMove -= new MouseEventHandler(OnDragCursorMouseMove); } if (processDrop) { // Find a destination slot, using the search elements to find a Slot that intersects with the current // mouse position. Slot destSlot = null; UIElement destElement = null; // Foreach of the search elements attempt to find a slot at the x and y position. foreach (var element in this.SearchElements.Where(se => se.Visibility == Visibility.Visible)) { var slot = (from v in VisualTreeHelper.FindElementsInHostCoordinates(this.MousePosition, element) where (v as Slot) != null select v as Slot).FirstOrDefault(); if (slot != null) { destElement = element; destSlot = slot; break; } } if (destSlot != null) { // Just use the first available, unless it is the same as the source slot. if (destSlot != this.SourceSlot && this.SourceSlot.IsDraggable && destSlot.IsDroppable) { SlotItem destItem = destSlot.Item; SlotItem sourceItem = this.SourceSlot.Item; SlotItemChangeEventArgs args = new SlotItemChangeEventArgs(destElement, this.SourceSlot, destSlot, sourceItem, destItem); this.SlotItemChange(args); // Only continue this processing if the change was not handled. if (!args.Handled) { // Clear the source slot item. this.SourceSlot.Item = null; // If the destination slot is not empty then swap the two items. if (destItem != null) { SlotItemChangingEventArgs srcArgs = new SlotItemChangingEventArgs(this.SourceSlot, sourceItem, destItem); this.SlotItemChanging(srcArgs); if (!srcArgs.Cancel) { this.SourceSlot.Item = destItem; } } SlotItemChangingEventArgs destArgs = new SlotItemChangingEventArgs(destSlot, destItem, sourceItem); this.SlotItemChanging(destArgs); if (!destArgs.Cancel) { destSlot.Item = sourceItem; } } } } } }
public SlotItemChangingEventArgs(Slot slot, SlotItem oldValue, SlotItem newValue) { this.Slot = slot; this.OldValue = oldValue; this.NewValue = NewValue; }
public SlotItemChangeEventArgs(UIElement destinationElement, Slot sourceSlot, Slot destinationSlot, SlotItem sourceValue, SlotItem destinationValue) { this.DestinationElement = destinationElement; this.SourceSlot = sourceSlot; this.DestinationSlot = destinationSlot; this.SourceValue = sourceValue; this.DestinationValue = destinationValue; }