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

Prevent link creation on override void AddLink(MyLink link)

4 Answers 123 Views
Diagram
This is a migrated thread and some comments may be shown as answers.
Sammy
Top achievements
Rank 1
Sammy asked on 08 Aug 2014, 07:56 AM
Hi,

I am overriding the Addlink method of my custom graphsource object that is derived the ObservableGraphSourceBase<NodeViewModelBase, MyLink>. I want to control the creation of the link  on certain conditions. If the conditions are met, I call the base.AddLink() method, otherwise I want to prevent the creation of link. How can I do this?

I tried not calling base.AddLink() but the link still appears on the diagram while it is not contained in the graphsource. How can I prevent or delete this link to be shown on the diagram?

Thanks,

Semih

4 Answers, 1 is accepted

Sort by
0
Sammy
Top achievements
Rank 1
answered on 11 Aug 2014, 12:42 PM
Any solution?
0
Martin Ivanov
Telerik team
answered on 12 Aug 2014, 01:37 PM
Hi Sammy,

I am not sure that I completely understand your requirement. Can you elaborate a little more on your scenario by telling me how you add the connection and where the link appears? Also if you could provide me with a sample project it would be helpful for better understanding your case and assist you further.

However, you can use the ConnectionManipulationCompleted event of the diagram to prevent creation (and any other manipulation) of a connection. Here is an example in code:

private void diagram_ConnectionManipulationCompleted(object sender, ManipulationRoutedEventArgs e)
{
    if( your condition is met )
    {
        // cancel the connection's manipulation
        e.Handled = true;
    }
}  

I hope this helps.

Regards,
Martin
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
Sammy
Top achievements
Rank 1
answered on 13 Aug 2014, 07:05 AM
Hi Martin,

Firstly, I use MVVM pattern for the project. What I want to do is, when the user grabs a connection from a connector and attaches it to a connector of another shape, I want to cancel the connection creation if the user tries to connect the connection to a connector of a shape that I don't want. To provide this I tried overriding the AddLink method. In the method I can detect the condition but, how can I remove or delete the connection that is created with the last move? I tried to change the visibility but since it is not added by calling the base.Addlink() method I cannot manipulate it at all. But the arrow remains on the diagram somehow.(non selectable, just visually there).

How can I remove/hide the link shape that I don't want to be added.

public override void AddLink(TransitionShapeViewModel link)
        {
            if (link.Source.IsArtificial)
            {
                link.Visibility = Visibility.Collapsed;
                link.Refresh();
                return;
            }
 
            if (!link.IsConstraint)
            {
                if (link.SourceConnectorPosition == "MiddleCenter" || link.TargetConnectorPosition == "MiddleCenter")
                {
                    link.Visibility = Visibility.Collapsed;
                    link.Refresh();
                    return;
                }
 
                link.Transition.TargetState = link.Target.State;
                link.Transition.SourceState = link.Source.State;
 
                if (!link.Target.ParentComponent.Component.Transitions.Contains(link.Transition))
                {
                    link.Target.ParentComponent.Component.Transitions.Add(link.Transition);
                }
 
                if (!link.Target.ParentComponent.TransitionShapeViewModels.Contains(link))
                {
                    link.Target.ParentComponent.TransitionShapeViewModels.Add(link);
                }
            }
 
            base.AddLink(link);
        }
0
Martin Ivanov
Telerik team
answered on 15 Aug 2014, 03:42 PM
Hello Sammy,

The AddLink method will be called only if you add new connection in the diagram (therefore in the GraphSource). However, if you add connection and you doesn't call the base.AddLink() method, this new connection will be included in the diagram, but won't be synchronized with the view model which will lead to incorrect behavior of the link.

In order to achieve your requirement you can use the ConnectionManipulationCompleted event that I mentioned in my last reply and implement your logic there. 

private void diagram_ConnectionManipulationCompleted(object sender, ManipulationRoutedEventArgs e)
{
    if (e.ManipulationStatus == ManipulationStatus.Attaching)
    {
        var targetShape = e.Shape as RadDiagramShapeBase;
        var connection = e.Connection as RadDiagramConnection;
        var node = targetShape.DataContext as Node;
        var link = connection.DataContext as TransitionShapeViewModel ;
 
         
        if ( your conditions are met) // the connection won't be added/moved
        {
            e.Handled = true;
        }
    }
}

As for removing a connection (link) from the diagram you can use the GraphSource's Links collection. The last connection in the collection is the last connection added in the diagram.

Regards,
Martin
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
Tags
Diagram
Asked by
Sammy
Top achievements
Rank 1
Answers by
Sammy
Top achievements
Rank 1
Martin Ivanov
Telerik team
Share this question
or