DragLeave handlers can modify AllowedEffects of following DragEnter handlers
- Dominant language
- C#
- Stars
- 7.7k
- Forks
- 1.3k
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 61
Description
* .NET Core Version: **3.0.100-preview-009812**
* Windows version: **1809**
* Does the bug reproduce also in WPF for .NET Framework 4.8? **Yes**
> Project maintainers will consider changes that improve the product or fix known bugs (please file issues to make bugs "known").
Re-reporting a bug I previously reported against Desktop Framework WPF which was decided not to get fixed because "it isn't broken enough to require a fix". My opinion differs so I'll try again to get this fixed.
**Problem description:**
DragLeave handlers can modify the AllowedEffects of the following DragEnter handler by modifying their DragEventArgs.Effects property. Can happen accidently when pushing all the different drag events through a common handler. The problem is transient (fixes itself on the next mouse move because the DragLeave handler won't be called again) but causes cursor flickering due to the incorrect AllowedEffects in DragEnter. If the DragLeave handler is in a 3rd party control you can't easily fix the cursor flickering.
Root cause is that effects are passed by-ref (input is AllowedEffects, [output is Effects](https://referencesource.microsoft.com/#PresentationCore/Core/CSharp/System/Windows/DragDrop.cs,1256)) to two drag handlers in succession in [DragDrop.cs, OleDragOver implementation](https://referencesource.microsoft.com/#PresentationCore/Core/CSharp/System/Windows/DragDrop.cs,1001) at [line 1024](https://referencesource.microsoft.com/#PresentationCore/Core/CSharp/System/Windows/DragDrop.cs,1024) and [line 1033](https://referencesource.microsoft.com/#PresentationCore/Core/CSharp/System/Windows/DragDrop.cs,1033).
The simplest fix would be to store the original effect variable before calling the DragLeave handler and restore it before calling the DragEnter handler.
**Actual behavior:**
AllowedEffects in DragEnter handler are wrong if the DragLeave handler was setting DragEventArgs.Effects - in the given example this manifests itself in a flickering cursor when starting a drag from the green area and moving the cursor between red and blue areas. When you have more complex behavior attached to D&D like a thumbnail following the cursor this can get even more obvious.
**Expected behavior:**
AllowedEffects in DragEnter handler should match those specified by the drag originator and be independent of whatever DragLeave handlers returned from their DragEventArgs.Effects
**Minimal repro:**
```xaml
```
```c#
using System.Windows;
using System.Windows.Input;
namespace MyWPFApp
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void StartDrag(object sender, MouseButtonEventArgs e)
{
DragDrop.DoDragDrop((DependencyObject)sender, "data", DragDropEffects.Copy | DragDropEffects.Link);
}
private void CopyDragHandler(object sender, DragEventArgs e)
{
e.Effects = e.AllowedEffects & DragDropEffects.Copy;
}
private void LinkDragHandler(object sender, DragEventArgs e)
{
e.Effects = e.AllowedEffects & DragDropEffects.Link;
}
}
}
```
Contributor guide
Assessment
This issue has not been assessed yet.