There are 4 ListBox, of three of which you can drag and drop items into the fourth, and not to move, but to copy. How to register events to host ListBox? ListBox events, from which to drag, registered. from one listbox:

<ListBox x:Name="listhelmets" Height="214" Width="248" ItemsSource="{Binding ListHelmets}" IsSynchronizedWithCurrentItem="True" Canvas.Left="464"Canvas.Top="37" PreviewMouseDown="helmet_MouseDown1" PreviewMouseLeftButtonDown="helmet_PreviewMouseLeftButtonDown" DragLeave="helmet_DragLeave" PreviewMouseMove="helmet_PreviewMouseMove" SelectedValuePath="protection"> <ListBox.ItemTemplate > <DataTemplate > <StackPanel Orientation="Horizontal"> <Image Source="{Binding Path=Image}" Width="56" Height="61"/> <TextBox Height="30" Width="30"> <Binding Path="protection" /> </TextBox> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> 

in that:

 <ListBox x:Name="listHero" Height="148" Width="158" <ListBox.ItemTemplate > <DataTemplate > <StackPanel Orientation="Horizontal"> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> 

for the first listbox:

  private void helmet_MouseDown1(object sender, MouseButtonEventArgs e) { _startPoint = e.GetPosition(null); } private void helmet_PreviewMouseMove(object sender, MouseEventArgs e) { if (e.LeftButton != MouseButtonState.Pressed) return; Point mousePos = e.GetPosition(null); Vector diff = startPoint - mousePos; if (Math.Abs(diff.X) <= SystemParameters.MinimumHorizontalDragDistance && Math.Abs(diff.Y) <= SystemParameters.MinimumVerticalDragDistance) return; var lst = sender as ListBox; var li = FindAnchestor<ListBoxItem>((DependencyObject)e.OriginalSource); Console.WriteLine("move " + li); if (li == null) return; var str = lst.ItemContainerGenerator.ItemFromContainer(li); var data = new DataObject("txt", str); var res = DragDrop.DoDragDrop(li, data, DragDropEffects.All); if (res == DragDropEffects.Move) (lst.ItemsSource as IList).Remove(str); } static T FindAnchestor<T>(DependencyObject current) where T : DependencyObject { do { if (current is T) return (T)current; current = VisualTreeHelper.GetParent(current); } while (current != null); return null; } UPDATE private void listHero_Drop(object sender, System.Windows.DragEventArgs e) { var o = e.Data.GetData("txt"); var lst = sender as ListBox; (lst.ItemsSource as IList).Add(o); e.Effects = DragDropEffects.Move; } private void ListHero_OnDragEnter(object sender, System.Windows.DragEventArgs e) { if (e.Data.GetDataPresent("txt")) e.Effects = DragDropEffects.Move; }` 

    1 answer 1

    how to register events to host listbox?

    in xaml add Drop and DragEnter

     <ListBox Drop="ListBox_Drop" DragEnter="ListBox_DragEnter" /> 

    in cs add the code of event handlers, like this:

     void ListBox_Drop(object sender, DragEventArgs e) { var o = e.Data.GetData(...) as ...; ... e.Effects = DragDropEffects....; } void ListBox_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(...)) e.Effects = DragDropEffects....; } 

    The code that should be in the handlers is in the example here .


    UPDATE: v1 , drag and drop rows - replaced by the next.
    UPDATE: v2, drag and drop objects of different types.

     <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Width="300" Height="300"> <StackPanel> <ListBox ItemsSource="{Binding Items1}" Height="100" PreviewMouseLeftButtonDown="DragList_PreviewMouseLeftButtonDown" PreviewMouseMove="DragList_PreviewMouseMove"/> <ListBox ItemsSource="{Binding Items2}" Drop="ListBox_Drop" DragEnter="ListBox_DragEnter" Background="LightYellow" Height="200" AllowDrop="True" /> </StackPanel> </Window> 

     interface IData { } class Data1 : IData { public override string ToString() { return "Data1"; }} class Data2 : IData { public override string ToString() { return "Data2"; }} partial class MainWindow : Window { public MainWindow() { Items1 = new ObservableCollection<object>() { new Data1(), new Data2() }; Items2 = new ObservableCollection<object>(); this.DataContext = this; } public ObservableCollection<object> Items1 { get; set; } public ObservableCollection<object> Items2 { get; set; } Point startPoint; void DragList_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { startPoint = e.GetPosition(null); } void DragList_PreviewMouseMove(object sender, MouseEventArgs e) { if (e.LeftButton != MouseButtonState.Pressed) return; Point mousePos = e.GetPosition(null); Vector diff = startPoint - mousePos; if (Math.Abs(diff.X) <= SystemParameters.MinimumHorizontalDragDistance && Math.Abs(diff.Y) <= SystemParameters.MinimumVerticalDragDistance) return; var lst = sender as ListBox; var li = FindAnchestor<ListBoxItem>((DependencyObject)e.OriginalSource); Console.WriteLine("move " + li); if (li == null) return; var o = lst.ItemContainerGenerator.ItemFromContainer(li); var data = new DataObject(typeof(IData), o); var res = DragDrop.DoDragDrop(li, data, DragDropEffects.All); if (res == DragDropEffects.Move) (lst.ItemsSource as IList).Remove(o); } void ListBox_Drop(object sender, DragEventArgs e) { var o = e.Data.GetData(typeof(IData)); var lst = sender as ListBox; (lst.ItemsSource as IList).Add(o); e.Effects = DragDropEffects.Move; } void ListBox_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(typeof(IData))) e.Effects = DragDropEffects.Move; } static T FindAnchestor<T>(DependencyObject current) where T : DependencyObject { do { if (current is T) return (T)current; current = VisualTreeHelper.GetParent(current); } while (current != null); return null; } } 
    • this is understandable, but what about the body? - Nick Shepard
    • @NickShepard "what is in the body?" - added in response. and a link to a working example. - Stack
    • in the example I replaced the Canvas with a Listbox, after that the var p = e.GetPosition (c); // ListBox.SetLeft (img, pX); // ListBox.SetTop (img, pY); // c.Children.Insert (0, img); because listbox has no such methods. but the element moves from the first list box, disappears, which means it has moved, I suppose! and how to make the picture appear in the second list box and be seen? @Stack - Nick Shepard
    • @NickShepard added code to the response. - Stack
    • everything is simpler if it is a string, and I have three different classes in three lists - Helmet, Weapon and Armature with the corresponding picture and unique property - each list box will bind to the collection and the receiving list box must be able to accept all these type elements @Stack - Nick Shepard