I was working on another project and thought I was done until I tested the drag and drop capability (following a code example) and it failed to work as expected.
I created a simple WPF project on .NET 8 to test drag and drop, and it fails to work as well. Any know the secret to making this work?
Here is the MainWindow.xaml
markup:
<Window x:Class="TestDragAndDrop.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TestDragAndDrop"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<TextBox x:Name="txtTextBox1" AllowDrop="True" HorizontalAlignment="Left" Margin="201,236,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="319" DragOver="TextBox_DragOver" Drop="TextBox_Drop"/>
</Grid>
</Window>
Here is the code behind MainWindow.xaml.cs
:
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace TestDragAndDrop;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void TextBox_DragOver(object sender, DragEventArgs e)
{
e.Effects = DragDropEffects.All;
e.Handled = true;
}
private void TextBox_Drop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
if (files.Length > 0)
{
txtTextBox1.Text = files[0];
}
}
}
}
I even asked the AI to do this, and it gave me similar code – same events, but the events do not actually fire. What is going on? Does .NET 8 not support drag & drop for WPF?
I’m at a loss.
This is easy to reproduce (I did it in the latest version of VS2022 17.10.3)
2
Ok, I have a solution for this problem.
It’s simple:
Replace all your _DragOver events with _PreviewDragOver
Replace all your _Drop events with _PreviewDrop
Apparently, DragOver & Drop events are not supported anymore (go figure).
Here is the MainWindow.xaml markup:
<Window x:Class="TestDragAndDrop.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TestDragAndDrop"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<TextBox x:Name="TextBox" AllowDrop="True" HorizontalAlignment="Left" Margin="201,236,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="319" PreviewDragEnter="TextBox_PreviewDragEnter" PreviewDragOver="TextBox_PreviewDragOver" PreviewDrop="TextBox_PreviewDrop" />
</Grid>
</Window>
And the corrected code behind w/ the correct events:
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace TestDragAndDrop;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void TextBox_PreviewDragOver(object sender, DragEventArgs e)
{
e.Effects = DragDropEffects.All;
e.Handled = true;
}
private void TextBox_PreviewDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
if (files.Length > 0)
{
TextBox.Text = files[0];
}
}
}
}
I hope this helps others.