Hi,
I am trying to write a small framework of my own by wrapping (trying to) the infragistics DragDrop Framework in an attached behavior/ blend behavior.
The difference between the two is while attahced behavior is static blend behavior is instance based. Anyways, coming to the main topic.
I know the following things:
1) To make an UIElement drag source the following code is required. I am fine with this:
<ig:DragDropManager.DragSource> <ig:DragSource IsDraggable="True" DragChannels="Animal,Human" Drop="DragSource_Drop"> </ig:DragSource> </ig:DragDropManager.DragSource>
2) To make an UIElement a drop target the following code is required. I am fine with this too:
<ig:DragDropManager.DropTarget> <ig:DropTarget IsDropTarget="True" DropChannels="Animal"/> </ig:DragDropManager.DropTarget>
3) In the code behind, add the following handler:
private void DragSource_Drop(object sender, Infragistics.DragDrop.DropEventArgs e) { }
This is a spoiler. as this adds code behind. So I went for attahced behavior. Also, why not take the data required in the drag drop operations frm the ViewModel classes. This will give ViewModel s more control of the view. This idea made me create a couple of interfaces called IDragSource and IDropTarget that would contain all the info required for dragdrop operation.
So here goes my code, inside the behavior class:
var dragSource = AssociatedObject.DataContext as IDragSource; if (null != dragSource) { var infragisticsDragSource = new DragSource() { IsDraggable = dragSource.IsDraggable, DragChannels = dragSource.DragChannels }; infragisticsDragSource.Drop += DragSource_Drop; DragDropManager.SetDragSource(AssociatedObject, infragisticsDragSource); }
var dropTarget = AssociatedObject.DataContext as IDropTarget; if (null != dropTarget) { var infragisticsDropTarget = new DropTarget() { DropChannels = dropTarget.DropChannels, IsDropTarget = dropTarget.IsDropTarget }; }
Clearly, I have the Drop handler in the behavior clas itself:
private void DragSource_Drop(object sender, DropEventArgs e) { foreach (FrameworkElement fe in e.DropTargetElements) { var dropTarget = fe.DataContext as IDropTarget; if (sender is IDragSource) dropTarget.OnDropped((sender as IDragSource).DropArgument); } }
I just cant make it work. Is it some silly mistake I am doing or something being overlooked by me. I have attached the demo application. Please note you will need System.Windows.Interactivity.dll for the blend behavior to work. If you hate blend, just remove that class from the project. No problem. I am fine with the attached behavior way too.
regards,
Anybody got a chance to look into this?
Hello Sid,
Thank you for contacting our community!
I have been looking into your sample project and modified it so that the user can drag and drop items to the ListBox controls on the right side. There are a couple of changes to be made so that the sample works as expected. In the DragDropAttached class the DragSource had been correctly set: DragDropManager.SetDragSource(item, infragisticsDragSource); The same should be done for the DropTarget, too. Otherwise the framework would not know where to put the item: DragDropManager.SetDropTarget(item, infragisticsDropTarget);
Another thing to notice is that the element could not be directly cast to IDragSource. The FrameworkElement’s DataContext can be used to get the IDragSource object.
private static void DragSource_Drop(object sender, DropEventArgs e)
{
foreach (FrameworkElement fe in e.DropTargetElements)
var dropTarget = fe.DataContext as IDropTarget;
FrameworkElement dropElement = (sender as DragSource).AssociatedObject as FrameworkElement;
if (dropElement != null && dropElement.DataContext is IDragSource)
dropTarget.OnDropped((dropElement.DataContext as IDragSource).DropArgument);
}
Please feel free to test the attached project and let me know if you have any questions or concerns.