Is it possible to drag and drop an item from one Listbox
to another? not remove but copy. from this Listbox
. Actually, I have three listboxes, they are quite similar and I need to be able to drop an item(one from each Listbox
) to Listbox
listHero
<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>
to this one
<ListBox x:Name="listHero" Height="148" Width="158"
<ListBox.ItemTemplate >
<DataTemplate >
<StackPanel Orientation="Horizontal">
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Drag and drop for first listbox:
private void helmet_MouseDown1(object sender, MouseButtonEventArgs e)
{
_startPoint = e.GetPosition(null);
}
private void helmet_PreviewMouseMove(object sender, MouseEventArgs e)
{
Point mousePos = e.GetPosition(null);
Vector diff = _startPoint - mousePos;
if (e.LeftButton == MouseButtonState.Pressed &&
(Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance))
{
var listBox = sender as ListBox;
var listBoxItem = listBox.SelectedItem;
DataObject dragData = new DataObject(_dropIdentifier, listBoxItem);
DragDrop.DoDragDrop(listBox, dragData, DragDropEffects.Move);
}
As MSDN says about enumeration DragDropEffects
:
In order to just copy items you should just change the value DragDropEffects
from Move
to Copy
:
private void helmet_PreviewMouseMove(object sender, MouseEventArgs e)
{
//the code omitted for the brevity
if (e.LeftButton == MouseButtonState.Pressed &&
(Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance))
{
//the code omitted for the brevity
DragDrop.DoDragDrop(listBox, dragData, DragDropEffects.Copy);
}
}
XAML:
<ListBox x:Name="DropList"
Drop="DropList_Drop"
DragEnter="DropList_DragEnter"
AllowDrop="True" />
C#:
private void DropList_DragEnter(object sender, DragEventArgs e)
{
if (!e.Data.GetDataPresent("myFormat") ||
sender == e.Source)
{
e.Effects = DragDropEffects.None;
}
}
private void DropList_Drop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent("myFormat"))
{
Contact contact = e.Data.GetData("myFormat") as Contact;
ListView listView = sender as ListView;
listView.Items.Add(contact);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.