Hi Walter,
I’m dragging objects from a treeview, that, in the diagram, should be represented as nodes. Thus, I implemented an ItemDrag Event handler for the tree view like this:
private void TreeViewWorkstepdefinitionsOnItemDrag(object sender, ItemDragEventArgs e)
{
// the Tag attribute of the tree node holds the corresponding business model of the tree node
var dragDropData = (e.Item as TreeNode).Tag;
if (e.Button == MouseButtons.Left || e.Button == MouseButtons.Right)
{
this.DoDragDrop(dragDropData, DragDropEffects.Copy);
}
}
I then implemented a listener for the Drop Event of the diagram:
private void DiagramOnDrop(object sender, System.Windows.DragEventArgs dragEventArgs)
{
dragEventArgs.Handled = true;
// dragging a workstepdefinition from the workstepdefinition treeview to create a new node in the diagram
if (dragEventArgs.Data.GetDataPresent(typeof(STOWorkstepdefinitionDTOImpl)))
{
// create a new view model for the dropped data and add it to the Diagram's node collection
var dragDropData = dragEventArgs.Data.GetData(typeof(STOWorkstepdefinitionDTOImpl)) as STOWorkstepdefinitionDTOImpl;
var node = new STOWorkstepdefinitionNodeData(dragDropData);
node.NodeCategory = "NewNodeTemplate";
this.WrapInModelTransaction("add node", () => diagram.Model.AddNode(node));
// set the drop position of the node
var currentPosition = dragEventArgs.GetPosition(diagram);
node.Location = diagram.Panel.TransformViewToModel(currentPosition);
diagram.Nodes.First(x => x.Data == node).IsSelected = true;
}
}
Plus, I implemente a listener for the DragOver Event to disable dropping onto an existing node like following:
private void DiagramOnDragOver(object sender, System.Windows.DragEventArgs dragEventArgs)
{
dragEventArgs.Handled = true;
if (this.Model.IsReadOnly)
{
dragEventArgs.Effects = System.Windows.DragDropEffects.None;
return;
}
dragEventArgs.Effects = System.Windows.DragDropEffects.Copy | System.Windows.DragDropEffects.Move;
var point = diagram.Panel.TransformViewToModel(dragEventArgs.GetPosition(diagram));
var node = diagram.Panel.FindPartAt<Node>(point, n => true, SearchLayers.All);
if (node != null)
{
dragEventArgs.Effects = System.Windows.DragDropEffects.None;
}
}
I then started to take a look at the DraggingTool but have no real glue what exactly I have to do to make a node appear as soon as the dragging is over the diagram area. As mentioned in my request, I read sth. about that one would have to create parts. But until now, I didn’t create parts (programmatically) and don’t know exactly how to create them and what to do with them if they are created.
I’m also totally unsure if the way i implemented the drag & drop is ok or if everything should be handled by the dragging tool (if this would be possible).
Kind regards,
Marc