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 .net8 project 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 .Net8 not support drag&drop for WPF ?
At a loss.
This is easy to reproduce (I did it in the latest version of VS2022 17.10.3)