1

I'm using the ListBoxDragDropTarget from the Silverlight Toolkit (April 2010) with SL 4.

I want to drag items from the list box onto a Label and handle the drop event there.

However it seems a bit complicated. The regular Drop event of the Label never gets fired. I suppose that is because the Silverlight Toolkit has its own way of handling Drag & Drop which is only partially compatible.

Looking arround I found the Microsoft.Windows.DragDrop.DropEvent and attached a handler to this event. And it worked!! I got the Drop event. However I'm not sure how to get to the real object that was dragged (a string).

I tried e.Data.GetData(typeof(string)) but I got nothing. Looking at the available formats there is a System.Windows.Controls.ItemDragEventArgs object. Inside this I found an array of System.Collections.ObjectModel.Selection which then has an Item property. I suppose in this Item property I find my object, but the whole method seems a bit fragile and I'm not convinced that is the official way to do this.

Is there any better way?

aKzenT
  • 7,775
  • 2
  • 36
  • 65

1 Answers1

0

U can also use another ListBox For ex: include namespace

 xmlns:toolKit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"

Let us add the “ListBoxDragDropTarget inside the Grid. Set the attribute “AllowDrop” to True. Once it is set to true, it will be able to catch the drop event inside the control. Now we will add a ListBox inside the ListBoxDragDropTarget and set properties whichever u like. suppose

 <toolKit:ListBoxDragDropTarget AllowDrop="True">
   <ListBox x:Name="customerListBoxMain" Height="200" Width="200"
       DisplayMemberPath="Name">
    <ListBox.ItemsPanel>
      <ItemsPanelTemplate>
           <StackPanel Orientation="Vertical"/>
      </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
   </ListBox>
  </toolKit:ListBoxDragDropTarget>

And add another ListBox

  <toolKit:ListBoxDragDropTarget AllowDrop="True">
    <ListBox Height="200" Width="200" DisplayMemberPath="Name">
   <ListBox.ItemsPanel>
    <ItemsPanelTemplate>
      <StackPanel Orientation="Vertical"/>
    </ItemsPanelTemplate>
   </ListBox.ItemsPanel>
   </ListBox>
  </toolKit:ListBoxDragDropTarget>

Now to fetch some data and set it to the Source of the first ListBox from code behind. Here is the sample code:

  public partial class MainPage : UserControl
  {
     public MainPage()
     {
        InitializeComponent();

        customerListBoxMain.ItemsSource = PersonDataProvider.GetData();
     }
   }

It's Done..

R76
  • 446
  • 6
  • 25