Click here to Skip to main content
15,902,937 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been following the series of wpf diagram posts to accomplish what i need to do at the project I´m working on at the moment.

Right now Im stuck with a little problem that you might have the answer straightway.
What I´m trying to do is create a line connection between two items on the canvas.
It works fine if i use the mouse, but I need to do that in the background.
Lets say I want to do that when i load the form.

I can load the elements and drawing the line but the problem is
the line and elements are not on connect on each other and thats is my problem.

Here is pice of code im using.

AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(canvas);

ConnectorAdorner cAdorner = new ConnectorAdorner(canvas, sourceConnector);

adornerLayer.Add(cAdorner);

cAdorner.HitConnector = sinkConnector;

cAdorner.pathGeometry = cAdorner.GetPathGeometry(new Point(211.0, 181.54));

cAdorner.HitDesignerItem = sinkConnector.parentDesignerItem;
cAdorner.hitDesignerItem.IsDragConnectionOver = true;

Connection connection = new Connection(sourceConnector, sinkConnector);

this.canvas.Children.Insert(0, connection);

if (adornerLayer != null)
{
adornerLayer.Remove(cAdorner);
}

I will really appreciate if someone can help me with that or just point in some direction.

thanks a lot
Posted
Comments
Sergey Alexandrovich Kryukov 9-Oct-14 11:35am    
The problem is not clear. Why don't they connect?
—SA

1 solution

Your problem is unclear (probably you only need to calculate the line coordinates more accurately :-)), but I can suggest you the alternative idea. I am not sure what kind of diagramming you do, so I cannot be 100% sure my idea is useful, but for typical diagramming design it could be useful. Just try to analyze my considerations below.

I would suggest, instead of using an adorner layer (good idea itself; please see below), treating the connecting lines as first-class citizen, on par with the diagram elements they connect. For example, the user should be able to include the lines in group selection, select, remove (but not move; the lines will follow the other diagram element movement). In other words, make you lines using (System.Windows.Shapes.Line; curved lines would also be an option).

Why? Because this is the logic of diagramming. First of all due to the diagramming conception. Connecting lines are truly the part of the diagram, they are the firs-class diagram objects, they can have properties associated with them, and so on. Another reason is technical. It's really difficult to connect shapes, if they are not simple (say, not round or rectangular); it's hard to calculate the point of intersection between the shape and connecting line. But you could just connect the centers and mask the invisible part of line by the non-transparent shape. However, this won't work if you need to draw an arrow head. Anyway, there are other reasons for such design.

Now, a word on the adorner layers. It's important to isolate visual properties related to the document itself (your diagram objects) and visual aspects related to the user's manipulations in UI, which are secondary in relation to the document: zoom/pan, selection, etc. The adorner layers are great to show these secondary visual elements, such as selection marks, selected area during drag-style selection, etc.

—SA
 
Share this answer
 
Comments
Decarlos 9-Oct-14 12:49pm    
Hi Sergey,

Thanks for your comment.

As i sad before it works fine if I drag an drop the line using the mouse, i just cant do that on onload.
The positions should be fine, i just not sure if i am able to do that on onload of the form with the pice of code i am using.

thanks a lot
Sergey Alexandrovich Kryukov 9-Oct-14 13:13pm    
My advice is still valid.
I don't understand what do you mean by "onload" and why it can be a problem. Anyway, in my approach, the line will behave just like other diagram elements. Think about it.
—SA
Decarlos 9-Oct-14 20:34pm    
Hi Sergey,
Sorry for not been totally clear.
When i say "onload" I mean when I open the diagram.
Thats because Im saving my items on a database so when open the diagram I have to load these items(shapes and lines )from the database and build the diagram.
Thanks for your advice I will consider that.
Sergey Alexandrovich Kryukov 10-Oct-14 12:24pm    
All right; this is diagram population. Anyway, there is no a difference where do you manipulate the objects on a canvas. If the connecting lines are part of the document and are first-class citizen objects, it comes naturally; my advice is valid.

Consider following it and accepting the answer formally (green "Accept" button).

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900