0
votes

Is there a way to check, if the user is currently dragging a control in WPF? Events to handle start-drag and drop don't do the trick in my current application. Of course, a simple workaround could be implemented by just setting a bool-flag, which is set during start-drag, so a drag-n-drop can be seen as active.

Unfortunately, that solution is not very robust, since you have to manually reset that flag on drop - and have to think of all the possibilities, which could cause a drop-event to never occur (user presses ESC, not dropped into an allowDrop-control, ...).

1

1 Answers

2
votes

I have a hacky sort of solution that I've come up with after spending lots of time trying to understand drag and drop in WPF.

The WPF InputManager which is "responsible for coordinating all of the input systems in WPF", does technically have a property that says whether or not a drag and drop operation is currently in progress.

/// <summary>
///     The InDragDrop property represents whether we are currently inside
///     a OLE DragDrop operation.
/// </summary>
internal bool InDragDrop

The above is from the official .NET Framework source code. Specifically InputManager.cs.

The problem is that, for some reason, it's declared as an internal property, so we can't access it... or at least we're not supposed to. internal and private members can still be accessed using refection. So InputManager.InDragDrop can technically be read by doing the following:

VB.NET

GetType(InputManager).GetProperty("InDragDrop", Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Instance).GetValue(InputManager.Current)

C#:

typeof(InputManager).GetProperty("InDragDrop", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(InputManager.Current);

I have used this and it does work, but its not an officially supported feature of WPF so use at your own risk. There's technically a chance that Microsoft could change the way InputManager works in the future and remove or rename this property, thought personally I doubt it.