Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

1 solution

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[^]
 
Share this answer
 
v2
Comments
JustForHelp 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.
<grid x:name="LayoutRoot" removed="White">
<toolkit:listboxdragdroptarget allowdrop="True">
<ListBox Name="lst" ItemsSource="{Binding}" Margin="36,23,0,132" HorizontalAlignment="Left" Width="131"
AllowDrop="True" >
<ListBox.ItemTemplate>
<datatemplate>
<stackpanel orientation="Horizontal">
<textblock text="{Binding Name}">
<textblock text="{Binding Age}">
<textblock text="{Binding Addr}">


</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>
<datatemplate>
<stackpanel orientation="Horizontal">
<textblock text="{Binding Name}">
<textblock text="{Binding Age}">
<textblock text="{Binding Addr}">


</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 26-Jul-12 8:37am    
And where do you start the drag routine? you only included the drop rotine in the code above.
JustForHelp 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
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900