Page Index Toggle Pages: 1 Send TopicPrint
Hot Topic (More than 10 Replies) Drag and drop (Read 10313 times)
binoo
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 12
Joined: Dec 20th, 2009
Drag and drop
Dec 22nd, 2009 at 9:13am
Print Post  
Hi,

I try to implement a drag and drop. I drag a node and drop it in a ListBox.

With the event MouseMove (or PreviewMouseMove) on the diagram like this :
Code
Select All
    private void diagram_MouseMove(object sender, MouseEventArgs e) {
      Diagram diag = sender as Diagram;
      if (e.LeftButton == MouseButtonState.Pressed) {
        DiagramItem diagItem = this.diagram.GetItemAt(e.GetPosition(this), false);

      }
    } 


I never pass in the if condition. It's normal ?
  
Back to top
 
IP Logged
 
binoo
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 12
Joined: Dec 20th, 2009
Re: Drag and drop
Reply #1 - Dec 22nd, 2009 at 9:40am
Print Post  
I try another solution too, with the event PreviewMouseMove of each node.

The method which create my node :
Code
Select All
    private void CreateNode(object obj, ref ShapeNode node) {
      node.Text = obj.ToString();
      node.TextFormat.LineAlignment = StringAlignment.Center;
      node.Tag = obj;
      node.Shape = this.CreateShape(obj.ToString());
      node.Font.Size = 9.0;
      node.ToolTip = obj.ToString();
      node.Brush = Brushes.Transparent;
      node.Pen = new Pen(Brushes.Black, 1.0);

      node.PreviewMouseMove += new MouseEventHandler(node_MouseMove);

      #region Pour gérer le double clic sur un ShapeNode

      MouseGesture OpenCmdMouseGesture = new MouseGesture();
      OpenCmdMouseGesture.MouseAction = MouseAction.LeftDoubleClick;

      MouseBinding OpenCmdMouseBinding = new MouseBinding();
      OpenCmdMouseBinding.Gesture = OpenCmdMouseGesture;
      OpenCmdMouseBinding.Command = this.DoubleClickCommand;

      node.InputBindings.Add(OpenCmdMouseBinding);

      #endregion
      #region Pour gérer le simple click sur un ShapeNode

      OpenCmdMouseGesture = new MouseGesture();
      OpenCmdMouseGesture.MouseAction = MouseAction.LeftClick;

      OpenCmdMouseBinding = new MouseBinding();
      OpenCmdMouseBinding.Gesture = OpenCmdMouseGesture;
      OpenCmdMouseBinding.Command = this.ClickCommand;

      node.InputBindings.Add(OpenCmdMouseBinding);

      #endregion

      node.AnchorPattern = new AnchorPattern(
        new AnchorPoint[] {
                new AnchorPoint(0, 50, true, false),
                new AnchorPoint(50, 100, false, true),
                new AnchorPoint(100, 50, false, true)
              }
      );

      // on ne veut pas voir les points d'accroches
      node.EnabledHandles = AdjustmentHandles.None;
      node.HandlesStyle = HandlesStyle.Invisible;

      // pour empécher le tracé de lien entre ShapeNode
      node.Locked = true;
    } 



As you can see, I add an InputBindings to the node for the MouseAction.LeftClick.
Without this InputBindings, all I have is the selection rectangle and the drag & drop don't work.

With this InputBindings, I can drag & drop in my ListBox, but the diagram don't understand that I succed my drag & drop when I come back over the diagram. Maybe due to the selection rectangle...
Undecided

Maybe this is not the good solution, so if you have got any sample.

Thanks.
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Drag and drop
Reply #2 - Dec 22nd, 2009 at 12:23pm
Print Post  
Hi,

If you need to start drag-and-drop with the diagram as a source, try this:

Code
Select All
void diagram_PreviewMouseMove(object sender, MouseEventArgs e)
{
	if (e.LeftButton == MouseButtonState.Pressed)
	{
		Point mp = e.GetPosition(diagram.DocumentPlane);
		var node = diagram.GetNodeAt(mp) as ShapeNode;
		if (node != null)
		{
			e.Handled = true;
			DragDrop.DoDragDrop(diagram, node.Text, DragDropEffects.Copy);
			if (diagram.Interaction != null)
				diagram.Interaction.CancelNow();
		}
	}
} 



I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
binoo
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 12
Joined: Dec 20th, 2009
Re: Drag and drop
Reply #3 - Dec 22nd, 2009 at 5:30pm
Print Post  
Thanks for your solution.

However, I've got a problem when I drop my node in my ListBox. When I drag one node, I've got two node in my ListBox.

Did you notice this problem ?
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Drag and drop
Reply #4 - Dec 23rd, 2009 at 8:26am
Print Post  
What does you listbox_DragDrop handler look like?
  
Back to top
 
IP Logged
 
binoo
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 12
Joined: Dec 20th, 2009
Re: Drag and drop
Reply #5 - Dec 23rd, 2009 at 9:23am
Print Post  
I've got this code :
Code
Select All
    private void LboxToDropNode_DragOver(object sender, DragEventArgs e) {
      if (e.Data.GetDataPresent(typeof(string)))
        e.Effects = System.Windows.DragDropEffects.Copy;
      else
        e.Effects = System.Windows.DragDropEffects.None;
    }

    private void LboxToDropNode_Drop(object sender, DragEventArgs e) {
      if (e.Data.GetDataPresent(typeof(string))) {
        e.Effects = System.Windows.DragDropEffects.Copy;
        // récupération de l'objet que l'on glisse
        string res = (string)e.Data.GetData(typeof(string));

        // utilisation de 'res'
        this.LboxToDropNode.Items.Add(res);
      }
    } 

  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Drag and drop
Reply #6 - Dec 23rd, 2009 at 10:09am
Print Post  
Is LboxToDropNode_Drop called twice after dropping the item?
  
Back to top
 
IP Logged
 
Hunter
Full Member
***
Offline



Posts: 194
Location: Sevastopol
Joined: Dec 1st, 2009
Re: Drag and drop
Reply #7 - Dec 23rd, 2009 at 1:57pm
Print Post  
Hi! I have the problem with drop. When I use in the NodeListView ShapeNode objects all works as need. But when I try to change the ShapeNode objects on the Custom objects and try to drop the object from the list to diagram I have an exception:
A first chance exception of type 'System.NullReferenceException' occurred in MindFusion.Diagramming.Wpf.dll
A first chance exception of type 'System.NullReferenceException' occurred in PresentationCore.dll
An unhandled exception of type 'System.NullReferenceException' occurred in PresentationCore.dll

Additional information: Object reference not set to an instance of an object.
Code
Select All
    /// <summary>
    /// Class represents nodes which are using in the diagram with some additional
    /// properties
    /// </summary>
    public class CustomNode: ShapeNode
    {
	  #region Data
	  NodeType m_nNodeType = NodeType.Bus;
	  int m_nInpNum = 0;
	  int m_nOutNum = 0;
	  #endregion //Data

	  #region Constructor
	  /// <summary>
	  /// Default constructor. Creates an empty object
	  /// </summary>
	  /// <param name="a_nNodeType">Sets the type of the node</param>
	  public CustomNode(NodeType a_nNodeType): base()
	  {
		m_nNodeType = a_nNodeType;
	  }
	  #endregion //Constructor

	  #region Properties
	  /// <value>
	  /// Gets type of the Node according to enum <see cref="NodeType"/>
	  /// </value>
	  public NodeType Type
	  { get { return m_nNodeType; } }
	  /// <value>
	  /// Gets or sets number of inputs of the node
	  /// </value>
	  public int InputNumber
	  {
		set { m_nInpNum = value; }
		get { return m_nInpNum; }
	  }
	  /// <value>
	  /// Gets or sets number of outputs of the node
	  /// </value>
	  public int OutputNumber
	  {
		set { m_nOutNum = value; }
		get { return m_nOutNum; }
	  }
	  #endregion //Properties
 


What's wrong?
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Drag and drop
Reply #8 - Dec 23rd, 2009 at 2:18pm
Print Post  
Hi,

Implement either the Clone method or a copy constructor for your custom class.

I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
Hunter
Full Member
***
Offline



Posts: 194
Location: Sevastopol
Joined: Dec 1st, 2009
Re: Drag and drop
Reply #9 - Dec 23rd, 2009 at 2:53pm
Print Post  
Thank's. Now all is ok.
  
Back to top
 
IP Logged
 
binoo
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 12
Joined: Dec 20th, 2009
Re: Drag and drop
Reply #10 - Dec 24th, 2009 at 10:10am
Print Post  
Stoyo wrote on Dec 23rd, 2009 at 10:09am:
Is LboxToDropNode_Drop called twice after dropping the item?


If I put a break point after :
Code
Select All
if (e.LeftButton == MouseButtonState.Pressed)
 



With a break point, I have only one item which is added to my ListBox, without break point I have two items...

I notice another thing. When I change the DragOver code like this :
Code
Select All
    private void LboxToDropNode_DragOver(object sender, DragEventArgs e) {
	if (e.Data.GetDataPresent(typeof(string)))
	  e.Effects = System.Windows.DragDropEffects.None;
	else
	  e.Effects = System.Windows.DragDropEffects.None;
    }
 


I change the DragDropEffects.Copy in DragDropEffects.None, I have only one item added in my ListBox. But the user don't know that he can drop here...

I send a sample project to your profil email adress.

Thank you.

  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Drag and drop
Reply #11 - Dec 24th, 2009 at 10:58am
Print Post  
From the call stack shown when a breakpoint is hit, I suppose WPF manages to send us a second PreviewMouseMove message during the execution of the DragDrop method because the diagram has acquired the mouse capture. It seems if you release it, it works as expected:

if (e.LeftButton == MouseButtonState.Pressed) {
     diagram.ReleaseMouseCapture();
     ...

I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
binoo
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 12
Joined: Dec 20th, 2009
Re: Drag and drop
Reply #12 - Jan 4th, 2010 at 1:29pm
Print Post  
Thank you ! Smiley
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint