简体   繁体   中英

Determine in code whether to allow a dragdrop drop using the Silverlight Toolkit

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
              {

              }


          };

just change the Effects to None (like you're doing) should be enough - a good example is some of the internal code in the treeview drag drop target in the toolkit itself (the SetEffects method)

http://silverlight.codeplex.com/SourceControl/changeset/view/57505#779753

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 DragEnter event handler, when it should have been done in the DragOver event handler. Changing the effects in DragEnter are like Rainier Wolfcastle's Radioactive Man goggles-- they do nothing.

So, the code that works is as follows:

 lbDragDrop.DragOver += (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
          {

          }


      };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM