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

How to prevent moving connection / link / line

1 Answer 187 Views
Diagram
This is a migrated thread and some comments may be shown as answers.
Daniel
Top achievements
Rank 1
Daniel asked on 04 Mar 2016, 12:06 PM
Hi!

On my RadDiagram control I have generated elements with connections. How to prevent moving connection?
I want to allow the user only to detach and attach the connection. Forbidden situation is visualised in forbiddenSituation.PNG.  If the user unpin the connection and he/she doesn't drag connection to another item, the connection shouldn't be changed. So all connections should have Target and Source element.

I use ConnectionManipulationCompleted event. When ManipulationStatus is Moved I want to Undo this, but the truth is only the previous change will be undone, not current connection moving.

I was thinking I can cache previous state of connection (target and source) on ConnectionManipulationStared and set those if on ConnectionManipulationCompleted target or source are null. But it doesn't work.

1 Answer, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 07 Mar 2016, 12:02 PM
Hi Daniel,

In order to achieve your requirement you can save the connection target and source shapes in the ConnectionManipulationStarted event handler and then reset them in the ConnectionManipulationCompleted handler. Here is an example in code:
public partial class MainWindow : Window
{
    private IShape connectionSource;
    private IShape connectionTarget;       
     
    public MainWindow()
    {
        InitializeComponent();
        this.diagram.ConnectionManipulationStarted += diagram_ConnectionManipulationStarted;
        this.diagram.ConnectionManipulationCompleted += diagram_ConnectionManipulationCompleted;
    }  
 
    void diagram_ConnectionManipulationStarted(object sender, ManipulationRoutedEventArgs e)
    {
        this.connectionSource = e.Connection.Source;
        this.connectionTarget = e.Connection.Target;
    }
 
    void diagram_ConnectionManipulationCompleted(object sender, ManipulationRoutedEventArgs e)
    {           
        if (e.ManipulationStatus != ManipulationStatus.Attaching)
        {               
           Dispatcher.BeginInvoke(new Action(() => {                   
                e.Connection.Source = this.connectionSource;
                e.Connection.Target = this.connectionTarget;                   
 
                this.connectionSource = null;
                this.connectionTarget = null;
            }));
        }         
    }
}
As I understand you want to move the connection only if its reattached to a shape. In this case you can check for the ManipulationStatus.Attaching as demonstrated in the code snippet. Keep in mind that you will need to slightly delay the connection reattaching, otherwise it is too early for setting a new source and target. This is why there is a Dispatcher used in the code snippet above.

Please try this approach and let me know if it works for you.

Regards,
Martin
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
Diagram
Asked by
Daniel
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Share this question
or