To write this article I was using this web page.
For this example I was using code behind, since I didn't want to spend too much time on it.
So, first we will add two ListView's, I add them in a way to be resizible. Then, for first listview, from where you want to drag something, you need MouseMove event. Third, for second list view, where you want to drop something (in my case a simple string), Drop event, and AllowDrop has to be set to true.
Since it is recommended to start drag and drop after mouse moved a little bit, we need also PreviewMouseLeftButtonDown event.
So my XAML looks like this:
<Window x:Class="DragNdrop1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="1*"/> <ColumnDefinition Width="2*"/> </Grid.ColumnDefinitions> <ListView x:Name="listBox1" Grid.Column="0" MouseMove="listBox1_MouseMove" PreviewMouseLeftButtonDown="ListBoxItem_PreviewMouseLeftButtonDown"> <ListViewItem Content="List box 1"/> </ListView> <ListView Grid.Column="1" Margin="5,0,-5,0" Drop="ListBox_Drop" AllowDrop="True"> <ListViewItem Name="itemTest" Content="List box 2"/> </ListView> <GridSplitter Width="5" HorizontalAlignment="Right" VerticalAlignment="Stretch" Background="Red" /> </Grid> </Window>
Code looks like this:
using System; using System.Windows; using System.Windows.Controls; using System.Windows.Input; namespace DragNdrop1 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public Point startPoint; public MainWindow() { InitializeComponent(); } private void ListBoxItem_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { startPoint = e.GetPosition(null); } private void ListBox_Drop(object sender, DragEventArgs e) { if (e.Data.GetDataPresent("myFormat")) { string dragString = e.Data.GetData("myFormat").ToString(); ListView listView = sender as ListView; listView.Items.Add(dragString); } } private void listBox1_MouseMove(object sender, MouseEventArgs e) { // Get the current mouse position Point mousePos = e.GetPosition(null); Vector diff = startPoint - mousePos; if (e.LeftButton == MouseButtonState.Pressed && (Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance || Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance)) { string dragString = "Yo! A test!"; // Initialize the drag & drop operation DataObject dragData = new DataObject("myFormat", dragString); DragDrop.DoDragDrop(itemTest, dragData, DragDropEffects.Move); } } } }
Things to notice are lines:
string dragString = "Yo! A test!"; // Initialize the drag & drop operation DataObject dragData = new DataObject("myFormat", dragString); DragDrop.DoDragDrop(itemTest, dragData, DragDropEffects.Move);
Here you can see dragData object which I am creating under "myFormat" string, and which I am then passing to DoDragDrop method.
Example you can download from here.