Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>i've tried create one using observablecollection, have a look</p> <pre><code> ObservableCollection&lt;Emp&gt; _empList = new ObservableCollection&lt;Emp&gt;(); public Window1() { InitializeComponent(); _empList .Add(new Emp("1", 22)); _empList .Add(new Emp("2", 18)); _empList .Add(new Emp("3", 29)); _empList .Add(new Emp("4", 9)); _empList .Add(new Emp("5", 29)); _empList .Add(new Emp("6", 9)); listbox1.DisplayMemberPath = "Name"; listbox1.ItemsSource = _empList; Style itemContainerStyle = new Style(typeof(ListBoxItem)); itemContainerStyle.Setters.Add(new Setter(ListBoxItem.AllowDropProperty, true)); itemContainerStyle.Setters.Add(new EventSetter(ListBoxItem.PreviewMouseLeftButtonDownEvent, new MouseButtonEventHandler(s_PreviewMouseLeftButtonDown))); itemContainerStyle.Setters.Add(new EventSetter(ListBoxItem.DropEvent, new DragEventHandler(listbox1_Drop))); listbox1.ItemContainerStyle = itemContainerStyle; } </code></pre> <p>Drag and drop process</p> <pre><code> void s_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { if (sender is ListBoxItem) { ListBoxItem draggedItem = sender as ListBoxItem; DragDrop.DoDragDrop(draggedItem, draggedItem.DataContext, DragDropEffects.Move); draggedItem.IsSelected = true; } } void listbox1_Drop(object sender, DragEventArgs e) { Emp droppedData = e.Data.GetData(typeof(Emp)) as Emp; Emp target = ((ListBoxItem)(sender)).DataContext as Emp; int removedIdx = listbox1.Items.IndexOf(droppedData); int targetIdx = listbox1.Items.IndexOf(target); if (removedIdx &lt; targetIdx) { _empList.Insert(targetIdx + 1, droppedData); _empList.RemoveAt(removedIdx); } else { int remIdx = removedIdx+1; if (_empList.Count + 1 &gt; remIdx) { _empList.Insert(targetIdx, droppedData); _empList.RemoveAt(remIdx); } } } </code></pre> <p>Note: </p> <ul> <li>one thing sucks in the implementation is since it use the <code>PreviewMouseLeftButtonDown</code> event, the dragged item looks like not selected</li> <li>and also for an easier implementation the drop target is the list box items and not the listbox itself - might need a better solution for this</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload