Skip to main content
Home Forums Silverlight Programming Silverlight Controls and Silverlight Toolkit Dragging from a ListBox to a Rectangle
1 replies. Latest Post by Mog Liang - MSFT on November 9, 2009.
(0)
edt
Member
1 points
3 Posts
11-05-2009 1:33 AM |
I'm using the drag & drop controls in the new Silverlight Toolkit (October2009). It seems well designed for dragging to and from ListBoxes, TreeViews, DataGrids and Charts.
But I need to drag an object from a ListBox to a Rectangle (or to a Grid or anything else representing a space on the screen). In the event handler I need to be able to get the object being dropped, and the object it was dropped onto (because I have more than one).
I was able to add msWindows:DragDrop.AllowDrop="True" to a Rectangle, and it then works as a drop target, but I can't work out how to add an event handler to the Rectangle when doing it this way.
It sounds easy enough but it's not working as expected. I've read Jafar Husain's post (http://themechanicalbride.blogspot.com/2009/10/silverlight-drag-drop-support-part-2.html), it's quite detailed but I still can't get it. And the sample code provided with the Toolkit is for Listbox -> Listbox/Chart.
Any ideas? Thanks in advance.
Mog Lian...
All-Star
16043 points
1,553 Posts
11-09-2009 2:02 AM |
Hi edt,
I write an sample showing how to drap and drop item to stackpanel by handling drop event. please have a try.
xaml
<Grid x:Name="LayoutRoot" Height="400" Width="600"> <StackPanel Orientation="Horizontal"> <controls:ListBoxDragDropTarget msWindows:DragDrop.AllowDrop="True"> <ListBox ItemsSource="{Binding}" FontSize="32"/> </controls:ListBoxDragDropTarget> <StackPanel Name="sp1" Width="300" msWindows:DragDrop.AllowDrop="True" Background="Azure"/> </StackPanel> </Grid>
code
public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); Loaded += new RoutedEventHandler(MainPage_Loaded); } void MainPage_Loaded(object sender, RoutedEventArgs e) { var list = new ObservableCollection<string> { "hello", "world" }; DataContext = list; //attach drop event sp1.AddHandler(DragDrop.DropEvent, new DragEventHandler(StackPanel_onDrop), true); } public void StackPanel_onDrop(object sender, DragEventArgs e) { var list = e.Data.GetData(e.Data.GetFormats()[0]) as SelectionCollection; foreach (var item in list) { sp1.Children.Add( new TextBlock { Text = item.Item.ToString() }); } e.Handled = true; } }
Thanks,