private void LbElenco_MouseMove(object sender, System.Windows.Input.MouseEventArgs e) { ListBox lb = sender as ListBox; if ((lb != null) && (e.LeftButton == MouseButtonState.Pressed) && (lb.SelectedItem != null)) { lbDragSource = lb; DragableObj sourceObj = lb.SelectedItem as DragableObj; DragDrop.DoDragDrop(lb, sourceObj, DragDropEffects.All); } }
private void LbElenco2_Drop(object sender, DragEventArgs e) { ListBox lb = sender as ListBox; if (lb != null) { if (e.Data.GetDataPresent(typeof(DragableObj))) { DragableObj draggedObj = (DragableObj)e.Data.GetData(typeof(DragableObj)); int index = -1; for (int i = 0; i < Elenco2.Count; i++) { var lbi = lbElenco2.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem; if (lbi == null) { continue; } if (IsMouseOverTarget(lbi, e.GetPosition((IInputElement)lbi))) { index = i; break; } } if (index >= 0) { Elenco2.Insert(index, draggedObj); } else { Elenco2.Add(draggedObj); } ((ObservableCollection <DragableObj>)lbDragSource.ItemsSource).Remove(draggedObj); } } }