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

时间:2015-12-27 12:02:48

标签: c# wpf drag-and-drop listbox

是否可以将项目从一个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}}

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