Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
715
Drah And Drop framework : Wrapping In behavior for MVVM friendly applications
posted

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,

DragDropFrameworkPOC.zip
Parents Reply Children