I’m using the SilverLight Toolkit to implement some drag/drop functionality in a Silverlight 4 web application. My drag source is a listbox, and I’ve got eighteen potential drop targets. I need to allow/disallow dropping based on a string value on the dragged object.
I have no problem doing the comparison and determining whether or not the item is allowed to be dropped on the target, however, I’m having trouble figuring out what the best event is to handle, and how to make it not accept the drop.
I’ve looked at the DragEnter event, and that looks like the best place to handle this, but I’m not quite sure what I need to do to make it not drop. Here’s a snippet of some of the code that I’ve tried, but it doesn’t prevent the drop:
lbDragDrop.DragEnter += (src, e) =>
{
VaultSocketViewModel vm = this.DataContext as VaultSocketViewModel;
ListBoxDragDropTarget target = src as ListBoxDragDropTarget;
ObservableCollection<ItemModel> listBoxBinding = vm.Slots[target.Name];
object data = e.Data.GetData(e.Data.GetFormats()[0]);
ItemDragEventArgs eventArgs = data as ItemDragEventArgs;
SelectionCollection coll = eventArgs.Data as SelectionCollection;
ItemModel newItem = coll.Select(t => t.Item).OfType<ItemModel>().FirstOrDefault();
if (!target.Name.StartsWith(newItem.ItemSlot)) // don't allow drop
{
e.Effects = Microsoft.Windows.DragDropEffects.None;
e.Handled = true;
}
else
{
}
};
Well, I was close. As @James Manning said in his answer, “just change the Effects to None … should be enough.”
So, true, as long as you do it in the right place. I had put my code to handle this in the
DragEnterevent handler, when it should have been done in theDragOverevent handler. Changing the effects inDragEnterare like Rainier Wolfcastle’s Radioactive Man goggles– they do nothing.So, the code that works is as follows: