none
ListView/ListBox multiple item selection drag and drop example? RRS feed

  • Question

  • Could someone please show me example code for doing drag and drop with multiple item selection list views / list boxes (I'm using a GridView, but I guess that doesn't really matter here)?

    Or a pointer to such code?

    Thanks,

    Koen

     

    PS

    I already saw code for single item selection dnd, but that's not what I'm interested in. It's more about the selection/drag starting logic that is not so simple with multiple item selection list boxes.

    Wednesday, February 20, 2008 3:50 PM

Answers

  • Here is an example:

    Code Snippet
    <Window x:Class="AnswerHarness.DragAndDropListBox"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    Title="DragAndDropListBox" Height="300" Width="300">
    <
    StackPanel>
    <
    ListBox Name="listBox" Width="200" Height="200" BorderBrush="Navy" BorderThickness="1">
    <
    x:Type TypeName="DependencyObject"/>
    <
    x:Type TypeName="Visual"/>
    <
    x:Type TypeName="UIElement"/>
    <
    x:Type TypeName="FrameworkElement"/>
    <
    x:Type TypeName="Control"/>
    </
    ListBox>

    <
    TextBox Margin="5" Name="textBox"/>
    </
    StackPanel>
    </
    Window>
    public partial class DragAndDropListBox : Window
    {
    Int32 prevTimestamp = 0;
    public DragAndDropListBox()
    {
    InitializeComponent();
    listBox.SelectionMode = SelectionMode.Multiple;
    listBox.PreviewMouseLeftButtonDown += (sender, e) =>
    {
    if (prevTimestamp == 0)
    {
    prevTimestamp = e.Timestamp;
    }

    Int32 diff = e.Timestamp - prevTimestamp;
    // This diff value is arbitrary, you might need to choose a better one.
    if (e.LeftButton == MouseButtonState.Pressed && diff > 5000)
    {
    String content = listBox.SelectedItems.ToString();
    DataObject dataObject = new DataObject();
    dataObject.SetData(DataFormats.StringFormat, content);
    DragDrop.DoDragDrop(listBox, dataObject, DragDropEffects.Copy);
    }
    };
    }
    }

    Hope this helps
    Friday, February 22, 2008 8:08 AM

All replies

  • Thanks, but that's not what I meant (that link is about lasso selection mode). I'm talking about selecting multiple items in a listbox/listview with control or shift and then starting a drag and drop operation that drags all the selected items.

    Thursday, February 21, 2008 7:19 AM
  • Here is an example:

    Code Snippet
    <Window x:Class="AnswerHarness.DragAndDropListBox"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    Title="DragAndDropListBox" Height="300" Width="300">
    <
    StackPanel>
    <
    ListBox Name="listBox" Width="200" Height="200" BorderBrush="Navy" BorderThickness="1">
    <
    x:Type TypeName="DependencyObject"/>
    <
    x:Type TypeName="Visual"/>
    <
    x:Type TypeName="UIElement"/>
    <
    x:Type TypeName="FrameworkElement"/>
    <
    x:Type TypeName="Control"/>
    </
    ListBox>

    <
    TextBox Margin="5" Name="textBox"/>
    </
    StackPanel>
    </
    Window>
    public partial class DragAndDropListBox : Window
    {
    Int32 prevTimestamp = 0;
    public DragAndDropListBox()
    {
    InitializeComponent();
    listBox.SelectionMode = SelectionMode.Multiple;
    listBox.PreviewMouseLeftButtonDown += (sender, e) =>
    {
    if (prevTimestamp == 0)
    {
    prevTimestamp = e.Timestamp;
    }

    Int32 diff = e.Timestamp - prevTimestamp;
    // This diff value is arbitrary, you might need to choose a better one.
    if (e.LeftButton == MouseButtonState.Pressed && diff > 5000)
    {
    String content = listBox.SelectedItems.ToString();
    DataObject dataObject = new DataObject();
    dataObject.SetData(DataFormats.StringFormat, content);
    DragDrop.DoDragDrop(listBox, dataObject, DragDropEffects.Copy);
    }
    };
    }
    }

    Hope this helps
    Friday, February 22, 2008 8:08 AM
  •  

    I don't see how this is gona work.

     

     String content = listBox.SelectedItems.ToString();

     gives not the selected elements in version 2005. Does this only work in Visual Studio 2008 ?

    Wednesday, May 28, 2008 11:11 AM