Skip to main content

Microsoft Silverlight

Answered Question Dragging from a ListBox to a RectangleRSS Feed

(0)

edt
edt

Member

Member

1 points

3 Posts

Dragging from a ListBox to a Rectangle

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 Liang - MSFT
Mog Lian...

All-Star

All-Star

16023 points

1,553 Posts

Answered Question

Re: Dragging from a ListBox to a Rectangle

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,

Mog Liang
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities