This is a migrated thread and some comments may be shown as answers.

Add connection point with 1 click and without ctrl key pressed

2 Answers 29 Views
Diagram
This is a migrated thread and some comments may be shown as answers.
Diego
Top achievements
Rank 1
Diego asked on 22 Mar 2016, 11:23 PM

Hello,

I need to add connection points to a certain connection and i need to do it with a single click and without the 'ctrl' key pressed. I've tried the AddConnectionPoint method but is not giving us the expected behavior  (apparently the points need to be added in some specific order). This is my code:

1.void Connection_MouseDown(object sender, MouseButtonEventArgs e)
2.{
3.     RadDiagramConnection Connection = sender as RadDiagramConnection;
4.
5.     var transformedPoint = this.diagram.GetTransformedPoint(Mouse.GetPosition(this.diagram));
6.
7.     Connection.AddConnectionPoint(e.MouseDevice.GetPosition(this.diagram));         
8.}

I've tried this aswell:

01.void Connection_MouseDown(object sender, MouseButtonEventArgs e)
02.{
03.       RadDiagramConnection Connection = sender as RadDiagramConnection;
04. 
05.       var transformedPoint = this.diagram.GetTransformedPoint(Mouse.GetPosition(this.diagram));
06.       Connection.AddConnectionPoint(e.MouseDevice.GetPosition(this.diagram));
07. 
08.       IList<Point> connPoints = Connection.ConnectionPoints.OrderBy(c => c.X).OrderBy(c => c.Y).ToList();
09.                 
10.       Connection.ConnectionPoints.Clear();
11.       foreach (Point point in connPoints)
12.       {
13.           Connection.AddConnectionPoint(point);
14.       }   
15.}

When i start to make points with one single click, this happens (Attached image).  

We need to add connection points the same way we do with they ctrl key pressed. But without the 'ctrl' key pressed.

Thank you

 

 

2 Answers, 1 is accepted

Sort by
0
Accepted
Petar Mladenov
Telerik team
answered on 25 Mar 2016, 09:48 AM
Hi Diego,

I am sending you a possible approach you can use. Basically this pretty much what the diagram does internally. It calculates the index at which the connection point should be inserted.

public MainWindow()
        {
            InitializeComponent();
            this.AddHandler(RadDiagramConnection.MouseLeftButtonDownEvent, new MouseButtonEventHandler(RadDiagramConnection_MouseDown_1), true);
        }
 
        private void RadDiagramConnection_MouseDown_1(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            FrameworkElement element = e.OriginalSource as FrameworkElement;
            if (element == null)
                return;
 
            RadDiagramConnection connection = element.ParentOfType<RadDiagramConnection>();
            if (connection == null)
                return;
 
            this.AddPoint(connection, e.MouseDevice.GetPosition(this.diagram));
        }
 
        private void AddPoint(RadDiagramConnection connection, Point p)
        {
            var points = new List<Point>(connection.ConnectionPoints);
            points.Insert(0, connection.StartPoint);
            points.Add(connection.EndPoint);
            var oldIsModified = connection.IsModified;
            var spot = new Rect(new Point(p.X - 8, p.Y - 8), new Size(16, 16));
            var index = spot.IntersectsLineAtIndex(points);
            if (index > -1)
            {
                var oldPoints = connection.ConnectionPoints.Clone();
                var newPoints = connection.ConnectionPoints.Clone();
                newPoints.Insert(index, p);
 
                var command = new UndoableDelegateCommand(CommandNames.ModifyConnectionConnectionPoints,
                    x =>
                    {
                        connection.SetConnectionPoints(newPoints);
                        connection.IsModified = true;
                        ((IConnection)connection).Update();
                    },
                    x =>
                    {
                        connection.SetConnectionPoints(oldPoints);
                        connection.IsModified = oldIsModified;
                        ((IConnection)connection).Update();
                    });
                this.diagram.UndoRedoService.ExecuteCommand(command);
            }
        }

However, I want to note 2 differences with the built-in adding of connection points:
-- by default, the connection  should be first selected before using Ctrl + Mouse Down over it
-- there is a tolerance 10 px around the connection. But in your case you rely on mouse down exactly on the connection which by default is 1-2 px wide - this might be frustrating for your users.

Regards,
Petar Mladenov
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
0
Diego
Top achievements
Rank 1
answered on 28 Mar 2016, 11:24 PM
Thank yo very much Petar, this works perfect for me. I only have to validate that the RadDiagramConnection IsSelected. Thanks again.
Tags
Diagram
Asked by
Diego
Top achievements
Rank 1
Answers by
Petar Mladenov
Telerik team
Diego
Top achievements
Rank 1
Share this question
or