How do I implement drag & drop in WPF

In WPF (Windows Presentation Foundation), implementing drag and drop functionality involves handling two main events: DragEnter and Drop. Below is a simple example that demonstrates how to enable drag and drop functionality in a WPF application, allowing users to drag text from one TextBox to another.

<![CDATA[ // XAML Code // C# Code-Behind using System.Windows; using System.Windows.Controls; using System.Windows.Input; namespace DragDropExample { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void SourceTextBox_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.Text)) e.Effects = DragDropEffects.Copy; else e.Effects = DragDropEffects.None; } private void SourceTextBox_Drop(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.Text)) { string text = e.Data.GetData(DataFormats.Text).ToString(); SourceTextBox.Text = text; } } private void TargetTextBox_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.Text)) e.Effects = DragDropEffects.Copy; else e.Effects = DragDropEffects.None; } private void TargetTextBox_Drop(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.Text)) { string text = e.Data.GetData(DataFormats.Text).ToString(); TargetTextBox.Text = text; } } } } ]]>

WPF drag and drop event handling user interface TextBox C#