In this post we saw the way to drop files in a WPF window, now we are going to see a way to drag and drop items between listboxes. Lets say tha we have 2 listboxes in a window and we like to take items from the first one and drop them in the second one. So, i think it’s time for some coding…open visual studio, create a new WPF project and add 2 listboxes like the following ones:
<ListBox Name="List1" Width="150" HorizontalAlignment="Left">
<ListBoxItem>1</ListBoxItem>
<ListBoxItem>2</ListBoxItem>
<ListBoxItem>3</ListBoxItem>
<ListBoxItem>4</ListBoxItem>
<ListBoxItem>5</ListBoxItem>
</ListBox>
<ListBox Name="List2" Width="150" HorizontalAlignment="Right" />
Now, we have created the 2 listboxes in which the left one, in the window, contains the items to be dragged and the right one the items to be dropped. We must handle 3 situations:
When the left mouse button is pressed upon a listbox item.
The left mouse button remains pressed and the item is “dragged”.
The left mouse button is released upon the second list and the item is “dropped”.
In order to handle the 1st situation we must handle the PreviewMouseLeftButtonDown event of the first listbox and if the the mouse hits a listbox item, then the drag operation proceeds. Create a handler for the first listbox’s PreviewMouseLeftButtonDown event. Now the first listbox should look like this:
<ListBox Name="List1" Width="150" HorizontalAlignment="Left"
PreviewMouseLeftButtonDown="List1_PreviewMouseLeftButtonDown">
<ListBoxItem>1</ListBoxItem>
<ListBoxItem>2</ListBoxItem>
<ListBoxItem>3</ListBoxItem>
<ListBoxItem>4</ListBoxItem>
<ListBoxItem>5</ListBoxItem>
</ListBox>
In the window’s code behind class add the following private variable:
private ListBoxItem _dragged;
In the handler function add the following code:
private void List1_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (_dragged != null)
return;
UIElement element = List1.InputHitTest(e.GetPosition(List1)) as UIElement;
while (element != null)
{
if (element is ListBoxItem)
{
_dragged = (ListBoxItem)element;
break;
}
element = VisualTreeHelper.GetParent(element) as UIElement;
}
}
In this procedure, we get the exact element that is pointed by the mouse pointer and search its parents until the listbox item object is found. If no such object is found, then the drag operation doesn’t proceeds.
Read more: Development Romance
<ListBox Name="List1" Width="150" HorizontalAlignment="Left">
<ListBoxItem>1</ListBoxItem>
<ListBoxItem>2</ListBoxItem>
<ListBoxItem>3</ListBoxItem>
<ListBoxItem>4</ListBoxItem>
<ListBoxItem>5</ListBoxItem>
</ListBox>
<ListBox Name="List2" Width="150" HorizontalAlignment="Right" />
Now, we have created the 2 listboxes in which the left one, in the window, contains the items to be dragged and the right one the items to be dropped. We must handle 3 situations:
When the left mouse button is pressed upon a listbox item.
The left mouse button remains pressed and the item is “dragged”.
The left mouse button is released upon the second list and the item is “dropped”.
In order to handle the 1st situation we must handle the PreviewMouseLeftButtonDown event of the first listbox and if the the mouse hits a listbox item, then the drag operation proceeds. Create a handler for the first listbox’s PreviewMouseLeftButtonDown event. Now the first listbox should look like this:
<ListBox Name="List1" Width="150" HorizontalAlignment="Left"
PreviewMouseLeftButtonDown="List1_PreviewMouseLeftButtonDown">
<ListBoxItem>1</ListBoxItem>
<ListBoxItem>2</ListBoxItem>
<ListBoxItem>3</ListBoxItem>
<ListBoxItem>4</ListBoxItem>
<ListBoxItem>5</ListBoxItem>
</ListBox>
In the window’s code behind class add the following private variable:
private ListBoxItem _dragged;
In the handler function add the following code:
private void List1_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (_dragged != null)
return;
UIElement element = List1.InputHitTest(e.GetPosition(List1)) as UIElement;
while (element != null)
{
if (element is ListBoxItem)
{
_dragged = (ListBoxItem)element;
break;
}
element = VisualTreeHelper.GetParent(element) as UIElement;
}
}
In this procedure, we get the exact element that is pointed by the mouse pointer and search its parents until the listbox item object is found. If no such object is found, then the drag operation doesn’t proceeds.
Read more: Development Romance