Page Index Toggle Pages: 1 Send TopicPrint
Hot Topic (More than 10 Replies) Interaction with links inside a locked ContainerNode (Read 1459 times)
Kostya
Junior Member
**
Offline


I Love MindFusion!

Posts: 59
Joined: Aug 5th, 2014
Interaction with links inside a locked ContainerNode
Apr 20th, 2023 at 9:05am
Print Post  
Hi,

I need to use one ContainerNode for all other diagram elements. Accordingly, as a background, it must be locked so that it cannot be selected and moved. It's good that the nodes inside it are getting locked as well. I can manually add new nodes, have context menus to edit them, etc. But at the same time, I still need to be able to draw links between the nodes, select existing links. How can this be achieved? That is, draw nodes and links and select them inside the locked container.

Thanks!
Kostya
  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3153
Joined: Oct 19th, 2005
Re: Interaction with links inside a locked ContainerNode
Reply #1 - Apr 20th, 2023 at 11:04am
Print Post  
Hi Kostya,

Instead of locking try disabling adjustment handles -

Code
Select All
container.EnabledHandles = AdjustmentHandles.None; 



and validating selection -

Code
Select All
void OnNodeSelecting(object sender, NodeValidationEventArgs e)
{
	e.Cancel = e.Node is ContainerNode;
} 



To allow selecting links in container set -

Code
Select All
diagram.HitTestPriority = HitTestPriority.ZOrder;
 



Regards,
Slavcho
Mindfusion
  
Back to top
 
IP Logged
 
Kostya
Junior Member
**
Offline


I Love MindFusion!

Posts: 59
Joined: Aug 5th, 2014
Re: Interaction with links inside a locked ContainerNode
Reply #2 - Apr 20th, 2023 at 1:54pm
Print Post  
Hi Slavcho,

Thanks much for the quick reply! The only thing that doesn't work now is adding a new node manually. Locking the container node before allowed to avoid creation of links inside it. Now, for this purpose I did like this

Code
Select All
            _containerNode.AllowIncomingLinks = false;
            _containerNode.AllowOutgoingLinks = false;
 


But how now to allow the creation of a new ShapeNode by drawing it in such a container? I have a custom behavior set for the diagram

Code
Select All
            CustomNodeType = typeof(ShapeNode);
            Behavior = Behavior.DrawNodes;
 


but it doesn't work in the container now.

Thanks,
Kostya
  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3153
Joined: Oct 19th, 2005
Re: Interaction with links inside a locked ContainerNode
Reply #3 - Apr 21st, 2023 at 6:34am
Print Post  
Hi Kostya,

Code
Select All
diagram.CustomNodeType = typeof(ShapeNode);
diagram.Behavior = Behavior.DrawNodes;
 



should be equivalent to setting Behavior.DrawShapes, but anyway both let me draw in the container in my test. Maybe check if you aren't also setting the CustomBehavior instance and its StartDraw override branching according to Locked value? Otherwise please attach a test project reproducing that.

Regards,
Slavcho
Mindfusion
  
Back to top
 
IP Logged
 
Kostya
Junior Member
**
Offline


I Love MindFusion!

Posts: 59
Joined: Aug 5th, 2014
Re: Interaction with links inside a locked ContainerNode
Reply #4 - Apr 21st, 2023 at 9:05am
Print Post  
Hi Slavcho,

You're right, indeed, Behavior.DrawShapes helps enable drawing ShapeNodes. Even though I specified CustomNodeType, Behavior.DrawNodes allowed me to draw TemplatedNodes only. Please let me show you my test project (attached). So, the container feels like it's locked, I can draw new nodes, but now I've lost the ability to draw links between nodes. How could I keep both node and link drawing (without affecting the container node, like it's still "locked")?

Thanks,
Kostya
  

MindfusionDiagramContainerTest.rar ( 1501 KB | 36 Downloads )
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3153
Joined: Oct 19th, 2005
Re: Interaction with links inside a locked ContainerNode
Reply #5 - Apr 21st, 2023 at 2:42pm
Print Post  
Hi Kostya,

Try assigning instance of this class to diagram.CustomBehavior:

Code
Select All
class IgnoreContainerBehavior : LinkShapesBehavior
{
	public IgnoreContainerBehavior(Diagram diagram) : base(diagram)
	{
	}

	public override InteractionState StartDraw(Point point)
	{
		var node = Diagram.GetNodeAt(point);
		if (node is ContainerNode)
		{
			return new InteractionState(
				CreateNode(), NodeAdjustmentHandle.ResizeBottomRight,
				MindFusion.Diagramming.Wpf.Action.Create);
		}

		return base.StartDraw(point);
	}
} 



LinkNodes / DrawNodes indeed create TemplatedNode objects using diagram's NodeTemplate. The CustomNodeType property is used when Behavior is set to Custom without assigning CustomBehavior instance.

Regards,
Slavcho
Mindfusion
  
Back to top
 
IP Logged
 
Kostya
Junior Member
**
Offline


I Love MindFusion!

Posts: 59
Joined: Aug 5th, 2014
Re: Interaction with links inside a locked ContainerNode
Reply #6 - Apr 21st, 2023 at 6:48pm
Print Post  
Hi Slavcho,

Thanks, it really worked! Let me just clarify one detail. The fact is that when creating a new node in this way, no matter how strange it may sound, I need to remove it under certain conditions. I do this OnNodeCreated:

Code
Select All
        private void Diagram_NodeCreated(object sender, NodeEventArgs e)
        {
            // ...
            Diagram.Nodes.Remove(e.Node);
        }
 


Even though the result of the operation is True, the node remains in the diagram. At the same time, for example, I can delete any other node at this moment:

Code
Select All
Diagram.Nodes.Remove(Diagram.Nodes[1]);
 



Is there some trick here? That is, should I do it later or delete it somehow differently?

Kostya
  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3153
Joined: Oct 19th, 2005
Re: Interaction with links inside a locked ContainerNode
Reply #7 - Apr 24th, 2023 at 8:04am
Print Post  
Hi Kostya,

If creating the new node by calling Factory.Create method, it might be added a second time by the interaction controller once operation is committed; try using constructor instead. The CreateNode shown in code above is a virtual method in base behavior class that returns instance without adding it to the diagram.

Otherwise if still using VirtualizingDiagram class, there could be a data item remaining in data source if using two-way binding.

In general, better set Cancel from NodeCreating validation event instead of deleting from NodeCreated. The latter might reroute links, create undo records, etc.

Regards,
Slavcho
Mindfusion
  
Back to top
 
IP Logged
 
Kostya
Junior Member
**
Offline


I Love MindFusion!

Posts: 59
Joined: Aug 5th, 2014
Re: Interaction with links inside a locked ContainerNode
Reply #8 - Apr 24th, 2023 at 9:25am
Print Post  
Hi Slavcho,

Got it about Factory.Create(). Yes, I came to this solution when I took your great advice to use a Diagram instead of a VirtualizingDiagram, which really helped avoid a lot of little problems. Now, I see that setting Cancel from NodeCreating instead of deleting from NodeCreated works to solve this problem, thanks!  Smiley

So, I do this and then everything else in NodeCreateCancelled without actually creating a new node. The only negative thing about this is that the cursor turns into a red icon when the user draws a new node. Is there any way to hide/customize it if I set e.Cancel=true when creating a node? The idea is not to show the user that the action has been cancelled, but to do it quietly.

Kostya
  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3153
Joined: Oct 19th, 2005
Re: Interaction with links inside a locked ContainerNode
Reply #9 - Apr 24th, 2023 at 11:04am
Print Post  
Hi Kostya,

Setting Cancel from NodeCreating shows Diagram.DisallowCursor, so you could reset that to normal pointer to hide the fact. Alternatively, check mouse button states through System.Windows.Input.Mouse and only cancel when the mouse button is up (so not during drag events).

Regards,
Slavcho
Mindfusion
  
Back to top
 
IP Logged
 
Kostya
Junior Member
**
Offline


I Love MindFusion!

Posts: 59
Joined: Aug 5th, 2014
Re: Interaction with links inside a locked ContainerNode
Reply #10 - Apr 24th, 2023 at 12:04pm
Print Post  
Slavcho, thanks much for your help! I'm glad all this can be resolved. I went the second way:

Code
Select All
            if (Mouse.LeftButton == MouseButtonState.Released)
                e.Cancel = true;
 



Kostya
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint