diff --git a/src/GongSolutions.WPF.DragDrop/DragDrop.cs b/src/GongSolutions.WPF.DragDrop/DragDrop.cs index b375f029..9a58f344 100644 --- a/src/GongSolutions.WPF.DragDrop/DragDrop.cs +++ b/src/GongSolutions.WPF.DragDrop/DragDrop.cs @@ -470,8 +470,8 @@ private static void DragSourceDown(object sender, DragInfo dragInfo, InputEventA return; } - // If the sender is a list box that allows multiple selections, ensure that clicking on an - // already selected item does not change the selection, otherwise dragging multiple items + // If the sender is a list box that allows multiple selections, ensure that clicking on an + // already selected item does not change the selection, otherwise dragging multiple items // is made impossible. if ((Keyboard.Modifiers & ModifierKeys.Shift) == 0 //&& (Keyboard.Modifiers & ModifierKeys.Control) == 0 // #432 @@ -783,7 +783,7 @@ private static void DropTargetOnDragOver(object sender, DragEventArgs e, EventTy // If the target is an ItemsControl then update the drop target adorner. if (itemsControl != null) { - // Display the adorner in the control's ItemsPresenter. If there is no + // Display the adorner in the control's ItemsPresenter. If there is no // ItemsPresenter provided by the style, try getting hold of a // ScrollContentPresenter and using that. UIElement adornedElement; @@ -1001,7 +1001,7 @@ private static DropTargetAdorner DropTargetAdorner get => dropTargetAdorner; set { - dropTargetAdorner?.Detatch(); + dropTargetAdorner?.Detach(); dropTargetAdorner = value; } } diff --git a/src/GongSolutions.WPF.DragDrop/DropTargetAdorner.cs b/src/GongSolutions.WPF.DragDrop/DropTargetAdorner.cs index ecfac16e..bff5b9bd 100644 --- a/src/GongSolutions.WPF.DragDrop/DropTargetAdorner.cs +++ b/src/GongSolutions.WPF.DragDrop/DropTargetAdorner.cs @@ -5,6 +5,8 @@ namespace GongSolutions.Wpf.DragDrop { + using JetBrains.Annotations; + public abstract class DropTargetAdorner : Adorner { public DropTargetAdorner(UIElement adornedElement, IDropInfo dropInfo) @@ -14,8 +16,9 @@ public DropTargetAdorner(UIElement adornedElement, IDropInfo dropInfo) this.IsHitTestVisible = false; this.AllowDrop = false; this.SnapsToDevicePixels = true; - this.m_AdornerLayer = AdornerLayer.GetAdornerLayer(adornedElement); - this.m_AdornerLayer.Add(this); + this.adornerLayer = AdornerLayer.GetAdornerLayer(adornedElement); + // can be null but should normally not be null + this.adornerLayer?.Add(this); } public IDropInfo DropInfo { get; set; } @@ -25,15 +28,20 @@ public DropTargetAdorner(UIElement adornedElement, IDropInfo dropInfo) /// public Pen Pen { get; set; } = new Pen(Brushes.Gray, 2); - public void Detatch() + public void Detach() { - if (!m_AdornerLayer.Dispatcher.CheckAccess()) + if (this.adornerLayer is null) + { + return; + } + + if (!this.adornerLayer.Dispatcher.CheckAccess()) { - m_AdornerLayer.Dispatcher.Invoke(this.Detatch); + this.adornerLayer.Dispatcher.Invoke(this.Detach); return; } - this.m_AdornerLayer.Remove(this); + this.adornerLayer.Remove(this); } internal static DropTargetAdorner Create(Type type, UIElement adornedElement, IDropInfo dropInfo) @@ -42,9 +50,11 @@ internal static DropTargetAdorner Create(Type type, UIElement adornedElement, ID { throw new InvalidOperationException("The requested adorner class does not derive from DropTargetAdorner."); } + return type.GetConstructor(new[] { typeof(UIElement), typeof(DropInfo) })?.Invoke(new object[] { adornedElement, dropInfo }) as DropTargetAdorner; } - private readonly AdornerLayer m_AdornerLayer; + [CanBeNull] + private readonly AdornerLayer adornerLayer; } } \ No newline at end of file