Search code examples
c#.netsilverlight-toolkitsilverlight-5.0

Drag Event Won't Fire for ListBox in Silverlight


Hopefully this is an easy question. I'm using the ListBoxDragDropTarget from the Silverlight Toolkit to set up drag and drop from one ListBox to another. I can't seem to get the event to fire. Here's my XAML code:

<toolkit:ListBoxDragDropTarget HorizontalAlignment="Left"
                               HorizontalContentAlignment="Stretch"
                               VerticalAlignment="Top"
                               VerticalContentAlignment="Stretch"
                               Margin="39,117,0,0"
                               Grid.Row="1"
                               AllowDrop='True'>
  <ListBox x:Name='columnHeadings'
           MinHeight='100'
           MinWidth='100'>
  </ListBox>
</toolkit:ListBoxDragDropTarget>

<toolkit:ListBoxDragDropTarget AllowDrop='True'
                               Grid.Column='1'
                               Grid.Row='1'
                               HorizontalContentAlignment="Stretch"
                               VerticalContentAlignment="Stretch"
                               VerticalAlignment="Center"
                               HorizontalAlignment="Left">
  <ListBox x:Name='customerFields'
           Grid.Column='1'
           Grid.Row='1'
           Visibility='Collapsed'
           Drop='CustomerFieldsDrop'>
  </ListBox>
</toolkit:ListBoxDragDropTarget>

And here's my event handler in the code behind page:

private void CustomerFieldsDrop(object sender, DragEventArgs e)
{
  MessageBox.Show(string.Format("Something was dropped!"));
}

As you can see, I was aiming for something real simple. I tried assigning an event handler to the parent ListBoxDragDropTarget for the customerFields List Box; ironically, that worked.

My purpose here is to allow a user to import a text file and get a listing of the file's column headers in one List Box and then "connect" them to data fields listed in the second List Box. So no list reordering or moving items from one list to another.

The columnHeadings.ItemsSource property is a string[] object. The customerFields.ItemsSource property is an IEnumerable<string> object.

Any insight would be appreciated.


Solution

  • I think the AllowDrop="True" and Drop="EventName" properties need to be within the same element to work. You probably have to set the AllowDrop property to "True" in the ListBox itself:

    <ListBox x:Name="customerFields" 
        Grid.Column="1" 
        Grid.Row="1" 
        Visibility="Collapsed"
        Drop="CustomerFieldsDrop"
        AllowDrop="True" 
    </ListBox> 
    

    Or add the Drop="CustomerFieldsDrop" property to the ListBoxDragDropTarget tag:

    <toolkit:ListBoxDragDropTarget AllowDrop='True'   
        Grid.Column='1'   
        Grid.Row='1'   
        HorizontalContentAlignment="Stretch"   
        VerticalContentAlignment="Stretch"   
        VerticalAlignment="Center"   
        HorizontalAlignment="Left"
        Drop="CustomerFieldsDrop">
    

    Either one should work...