Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic NodeListView and Drag and Drop (Read 4288 times)
Hunter
Full Member
***
Offline



Posts: 194
Location: Sevastopol
Joined: Dec 1st, 2009
NodeListView and Drag and Drop
Jul 9th, 2010 at 1:24pm
Print Post  
Hello, Stoyo! I have the problem. Earlier I use NodeListView with ShapeNode objects and all were OK. But now I need to change ShapeNode objects on my own objects which are described as following:
Code
Select All
BitmapImage bitmap = new BitmapImage(uri);
            Image img = new Image();
            img.Source = bitmap;
            img.Stretch = Stretch.Uniform; img.Width = 64; img.Height = 64;
            panel.Children.Add(img);
            TextBlock txtBlock = new TextBlock();
            txtBlock.Width = 80; txtBlock.TextWrapping = TextWrapping.Wrap;
            txtBlock.TextAlignment = TextAlignment.Center;
            txtBlock.Text = sLabel;
            panel.Children.Add(txtBlock);
            panel.Tag = a_nNodeType;
            m_ToolboxListView.Items.Add(panel);
 


And also I want to continue using NodeListView because it provides the Drag&Drop funcionality automatically. And I have the old code for drop event:
Code
Select All
        private void OnDiagram_Drop(object sender, DragEventArgs e)
        {
            AnchorPattern ap = null;
            if (e.Data.GetDataPresent(typeof(DraggedNode)))
            {
                m_Diagram.AutoResize = AutoResize.RightAndDown;
#if false
                DiagramNode node = m_Diagram.Nodes[m_Diagram.Nodes.Count - 1];
                if (node is TPDENode)
                {
                    TPDENode shapeNode = node as TPDENode;
                    shapeNode.CustomDraw = CustomDraw.Additional2;
                    shapeNode.ShadowProps.ShadowDepth = 5;
                    Point dropPnt = e.GetPosition(m_Diagram); and etc.
 


And I want to reuse this code. But I need to get access to the StackPanel.Tag. How can I do this? Thank you!
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: NodeListView and Drag and Drop
Reply #1 - Jul 9th, 2010 at 1:53pm
Print Post  
Hi,

What the Drop handler gets in this case should be a DiagramNodeAdapter whose UIElements points t the panel. So you can add an "if (node is DiagramNodeAdapter)" statement that casts node.UIElement to Panel and get the Tag value from it.

You might also explicitly create a DiagramNodeAdapter to add to the list view, and set its Tag instead of the panel's one.

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



Posts: 194
Location: Sevastopol
Joined: Dec 1st, 2009
Re: NodeListView and Drag and Drop
Reply #2 - Jul 9th, 2010 at 2:01pm
Print Post  
Yes. It is very helpful advice. Thank you very much! But I have the next problem. I modify my code as following:
Code
Select All
	  private void OnDiagram_Drop(object sender, DragEventArgs e)
	  {
		AnchorPattern ap = null;
		if (e.Data.GetDataPresent(typeof(DraggedNode)))
		{
		    m_Diagram.AutoResize = AutoResize.RightAndDown;
		    DiagramNode node = m_Diagram.Nodes[m_Diagram.Nodes.Count - 1];
		    if (node is DiagramNodeAdapter)
		    {
			  m_Diagram.Nodes.RemoveAt(m_Diagram.Nodes.Count - 1);
		    }
 


But I see my UIElement. It doesn't removed. But m_Diagram.Nodes.Count after RemoveAt is equals to 0. How to delete this UIElement? According to the node type I want to create the ShapeNode based object
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: NodeListView and Drag and Drop
Reply #3 - Jul 9th, 2010 at 2:29pm
Print Post  
The node does not exist yet in the diagram when Drop is raised. The Drop handler only sees the prototype node, but it will be cloned and added to the diagram after the handler returns. You might move this code to a NodeCreated handler to get access to the actual node.

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



Posts: 194
Location: Sevastopol
Joined: Dec 1st, 2009
Re: NodeListView and Drag and Drop
Reply #4 - Jul 9th, 2010 at 2:45pm
Print Post  
I have tried to do it. But I see the opposite situation. First I go to the NodeCreated event and after that I go to the drop handler. I'll try to fix my code and parse the NodeAdapter in the NodeCreated...
  
Back to top
 
IP Logged
 
Hunter
Full Member
***
Offline



Posts: 194
Location: Sevastopol
Joined: Dec 1st, 2009
Re: NodeListView and Drag and Drop
Reply #5 - Jul 9th, 2010 at 2:55pm
Print Post  
In the NodeCreated handler I wrote the following code:
Code
Select All
	  private void OnDiagram_NodeCreated(object sender, NodeEventArgs e)
	  {
		DiagramNode node = m_Diagram.Nodes[m_Diagram.Nodes.Count - 1];
		if (node is DiagramNodeAdapter)
		{
		    DiagramNodeAdapter adapter = node as DiagramNodeAdapter;
		    m_Diagram.Nodes.RemoveAt(m_Diagram.Nodes.Count - 1);
		    TPDENode tpdeNode = new TPDENode((NodeType)adapter.Tag);
		    m_Diagram.Nodes.Add(tpdeNode);
		}
	  }
 


And after that I go to the Drop event handler and use the old code:
Code
Select All
	  private void OnDiagram_Drop(object sender, DragEventArgs e)
	  {
		if (e.Data.GetDataPresent(typeof(DraggedNode)))
		{
		    m_Diagram.AutoResize = AutoResize.RightAndDown;
		    DiagramNode node = m_Diagram.Nodes[m_Diagram.Nodes.Count - 1];
		    AnchorPattern ap = null;
		    if (node is TPDENode)
		    {
			  TPDENode shapeNode = node as TPDENode;
			  shapeNode.CustomDraw = CustomDraw.Additional2;
			  shapeNode.ShadowProps.ShadowDepth = 5;
			  Point dropPnt = e.GetPosition(m_Diagram);
			  dropPnt.X /= m_Diagram.ZoomFactor / 100;
			  dropPnt.Y /= m_Diagram.ZoomFactor / 100;
			  Point alignedPnt = m_Diagram.AlignPointToGrid(dropPnt);
 


All is Ok. But the UIElement doesn't removed. Creates the ShapeNode based object and UIElement stays as is...
  
Back to top
 
IP Logged
 
Hunter
Full Member
***
Offline



Posts: 194
Location: Sevastopol
Joined: Dec 1st, 2009
Re: NodeListView and Drag and Drop
Reply #6 - Jul 9th, 2010 at 3:16pm
Print Post  
I solve the problem. The code in the NodeCreated must be the following and all will works:
Code
Select All
	 private void OnDiagram_NodeCreated(object sender, NodeEventArgs e)
	  {
		if (e.Node is DiagramNodeAdapter)
		{
		    DiagramNodeAdapter adapter = e.Node as DiagramNodeAdapter;
		    TPDENode tpdeNode = new TPDENode((NodeType)adapter.Tag);
		    m_Diagram.Nodes.Remove(e.Node);
		    m_Diagram.Nodes.Add(tpdeNode);
		}
	  }
 


Topic could be closed Smiley. Thank you
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint