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;
}
}
}
}
]]>
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?