是否可以将项目从一个Listbox
拖放到另一个Listbox
?不要删除而是复制。
来自Listbox
。实际上,我有三个列表框,它们非常相似,我需要能够删除一个项目(每个Listbox
一个)到<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>
listHero
<ListBox x:Name="listHero" Height="148" Width="158"
<ListBox.ItemTemplate >
<DataTemplate >
<StackPanel Orientation="Horizontal">
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</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);
}
拖放第一个列表框:
{{1}}
答案 0 :(得分:0)
As MSDN says 关于枚举DragDropEffects
:
为了只复制项目,您只需将DragDropEffects
的值从Move
更改为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);
}
}