using System; using System.Collections.Generic; using System.Linq; 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.Shapes; namespace DragDrop { public partial class Window1 : Window { TextBox dragSource = null; public Window1() { InitializeComponent(); } private void txt1_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { TextBox parent = (TextBox)sender; dragSource = parent; DataObject dragData = new DataObject(typeof(string), sender as TextBox); if (dragData != null) { System.Windows.DragDrop.DoDragDrop(parent, dragData, DragDropEffects.Move); } } private void txt1_PreviewDrop(object sender, DragEventArgs e) { var dropedData = e.Data.GetData(typeof(string)); MessageBox.Show(dropedData.ToString()); var sourceElement = (FrameworkElement)sender; var target = (sourceElement.InputHitTest(e.GetPosition(sourceElement)) as FrameworkElement); } } }
<Window x:Class="DragDrop.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="400" Width="525"> <Grid> <Canvas x:Name="canV" > <Label Canvas.Left="100" Canvas.Top="80" Content="Column A"></Label> <Label Canvas.Left="300" Canvas.Top="80" Content="Column B"></Label> <TextBox x:Name="txt1" Canvas.Left="100" Canvas.Top="120" Width="150" PreviewMouseLeftButtonDown="txt1_PreviewMouseLeftButtonDown" PreviewDrop="txt1_PreviewDrop"></TextBox> <TextBox Canvas.Left="300" Canvas.Top="120" Width="150" PreviewMouseLeftButtonDown="txt1_PreviewMouseLeftButtonDown" PreviewDrop="txt1_PreviewDrop"></TextBox> <TextBox Canvas.Left="100" Canvas.Top="160" Width="150" PreviewMouseLeftButtonDown="txt1_PreviewMouseLeftButtonDown" PreviewDrop="txt1_PreviewDrop"></TextBox> <TextBox Canvas.Left="300" Canvas.Top="160" Width="150" PreviewMouseLeftButtonDown="txt1_PreviewMouseLeftButtonDown" PreviewDrop="txt1_PreviewDrop"></TextBox> <TextBox Canvas.Left="100" Canvas.Top="200" Width="150" PreviewMouseLeftButtonDown="txt1_PreviewMouseLeftButtonDown" PreviewDrop="txt1_PreviewDrop"></TextBox> <TextBox Canvas.Left="300" Canvas.Top="200" Width="150" PreviewMouseLeftButtonDown="txt1_PreviewMouseLeftButtonDown" PreviewDrop="txt1_PreviewDrop"></TextBox> <TextBox Canvas.Left="100" Canvas.Top="240" Width="150" PreviewMouseLeftButtonDown="txt1_PreviewMouseLeftButtonDown" PreviewDrop="txt1_PreviewDrop"></TextBox> <TextBox Canvas.Left="300" Canvas.Top="240" Width="150" PreviewMouseLeftButtonDown="txt1_PreviewMouseLeftButtonDown" PreviewDrop="txt1_PreviewDrop"></TextBox> </Canvas> </Grid> </Window>
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)