gpt4 book ai didi

c# - 如何将项目从一个列表框拖放到另一个列表框

转载 作者:太空宇宙 更新时间:2023-11-03 15:28:16 28 4
gpt4 key购买 nike

是否可以将项目从一个 Listbox 拖放到另一个?不是删除而是复制。从这个 Listbox。实际上,我有三个列表框,它们非常相似,我需要能够将一个项目(每个 Listbox 中的一个)拖放到 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>

这个

 <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);
}

最佳答案

As MSDN says 关于枚举DragDropEffects:

  • All - 数据被复制,从拖动源中移除,并在其中滚动下降目标。
  • 复制 - 数据被复制到放置目标。
  • 链接 - 拖动源中的数据链接到放置目标。
  • 移动 - 将拖动源中的数据移动到放置目标。
  • - 放置目标不接受数据。
  • 滚动 - 滚动即将开始或当前正在放置目标中进行。

为了仅复制项目,您只需将值 DragDropEffectsMove 更改为 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);
}
}

关于c# - 如何将项目从一个列表框拖放到另一个列表框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34480200/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com