Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF C#: Rearrange items in listbox via drag and drop

I am trying to figure out how to move the items in a pre-populated listbox up and down via mouse drags.

I have looked at the Control.DoDragDrop method from microsoft's api, but I still can't get it to do anything.

I would appreciate any help since I am new to the visual studios environment.

like image 513
Aaron Avatar asked Jul 28 '10 05:07

Aaron


People also ask

Is WPF only C#?

WPF is used to build Windows client applications that run on Windows operating system. WPF uses XAML as its frontend language and C# as its backend languages.

What is WPF in C# used for?

Windows Presentation Foundation (WPF) is a UI framework that creates desktop client applications. The WPF development platform supports a broad set of application development features, including an application model, resources, controls, graphics, layout, data binding, documents, and security.

Is WPF still relevant 2022?

“WPF would be dead in 2022 because Microsoft doesn't need to be promoting non-mobile and non-cloud technology. But WPF might be alive in that sense if it's the best solution for fulfilling specific customer needs today. Therefore, having a hefty desktop application needs to run on Windows 7 PCs with IE 8.

Is WPF used anymore?

WPF is still one of the most used app frameworks in use on Windows (right behind WinForms).


1 Answers

I've tried creating one using ObservableCollection. Have a look.

    ObservableCollection<Emp> _empList = new ObservableCollection<Emp>();      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;     } 

Drag and drop process:

    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 < targetIdx)         {             _empList.Insert(targetIdx + 1, droppedData);             _empList.RemoveAt(removedIdx);         }         else         {             int remIdx = removedIdx+1;             if (_empList.Count + 1 > remIdx)             {                 _empList.Insert(targetIdx, droppedData);                 _empList.RemoveAt(remIdx);             }         }     } 

Note:

  • One thing that sucks in this implementation is that since it uses the PreviewMouseLeftButtonDown event, the dragged item does not look like a selected item.
  • 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.
like image 67
dnr3 Avatar answered Sep 23 '22 09:09

dnr3