Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
The problem I have encountered was how to get the dragged item and add it to the ListBox that accept the dragged item?Could anybody help me ? A similar sample presented is preferred.In addition ,I have to stress that the control I used is just a ListBoxDragDropTarget and a ListBox ,not two ListBoxDragDropTarget.Many thanks in advance.
Posted 26-Jul-12 3:08am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

WHats the exact problem you are facing? You can easily find multiple examples of the same with a quick earch on google. Heres one in WPF but it should be no problem to translate it to Silverlight:
 
Drag and Drop Items in a WPF ListView[^]
 
http://wpftutorial.net/DragAndDrop.html[^]
  Permalink  
v2
Comments
JustForHelp at 26-Jul-12 8:31am
   
My code is all here.But when I dragged the item in the left listboxDragDropTarge,it simply doesn't work.


<ListBox Name="lst" ItemsSource="{Binding}" Margin="36,23,0,132" HorizontalAlignment="Left" Width="131"
AllowDrop="True" >
<ListBox.ItemTemplate>







</ListBox.ItemTemplate>
</ListBox>

<Button Click="Add_click" Content="Add input" Height="20" Margin="161,262,159,18"></Button>
<ListBox Height="145" HorizontalAlignment="Left" Margin="235,23,0,0" Name="ListBoxTarget" VerticalAlignment="Top"
Width="128" AllowDrop="True" Drop="List_Drop" ItemsSource="{Binding}">
<ListBox.ItemTemplate>







</ListBox.ItemTemplate>
</ListBox>

 

 
namespace MyTrailListBox
{
public class Person
{
public string Name { get; set; }
public string Age { get; set; }
public string Addr { get; set; }
public Person(string name, string age, string addr)
{
Name = name;
Age = age;
Addr = addr;
}
}
}
 

 
namespace MyTrailListBox
{
public class Persons:ObservableCollection<Person>
{
public Persons()
{
Add(new Person("Aimy", "17", "Hubei"));
Add(new Person("John", "19", "Wuhan"));

}
}
}
 

namespace MyTrailListBox
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
this.DataContext = p;
ListBoxTarget.DataContext = p2;
}
public Persons p=new Persons();
public Persons p2 = new Persons();
private void Add_click(object sender, RoutedEventArgs e)
{
p.Add(new Person("Good", "20", "sdfa"));
p2.Add(new Person("Evening ", "20", " suohe"));
 
}
IEnumerable<Person> selectedItem;
private void List_Drop(object sender, DragEventArgs e)
{
ItemDragEventArgs itemDrag;
itemDrag= e.Data.GetData(e.Data.GetFormats()[0]) as ItemDragEventArgs;
SelectionCollection selectionCollection = itemDrag.Data as SelectionCollection;
if (selectionCollection != null)
{
selectedItem = selectionCollection.Select(selection => selection.Item).OfType<Person>();
}
var data = (ObservableCollection<Person>)selectedItem;
foreach (var item in data)
p2.Add(item);
e.Handled = true;
}
}
}
Kenneth Haugland at 26-Jul-12 8:37am
   
And where do you start the drag routine? you only included the drop rotine in the code above.
JustForHelp at 26-Jul-12 22:24pm
   
What's the meaning? I just think the only task is to get the dragged element.So we just need to deal with the drop event.Is is right?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 350
1 Jochen Arndt 190
2 Richard MacCutchan 135
3 Sergey Alexandrovich Kryukov 130
4 DamithSL 95
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,310


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 26 Jul 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100