is there a way to manage the connections between items , for example I want user not to be able to create more than "n" connections to an item , one thing that came in my mind was to handle the connection created event , but I did not suceed in handling it , because there wasn't such an event. Any ideas ?
Best regards,
Yoan
13 Answers, 1 is accepted
Currently, there is no straightforward way to achieve this. I have logged this as a feature request in PITS here so you can track its progress.
Greetings,Alex Fidanov
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
What about connection created event ?
When User creates a connection between two shapes.
The closest event to what you are looking for is the ItemsChanged event, however it is raised too early to handle this behavior.
Regards,Alex Fidanov
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
you are right, the event get raised too early & for each item change. So this is not a good choice
You need to do something for this, there should be unique Events
1. When your creating connections
2. When connection created
3. double or single click events for connection
4. connection modified or modifying
These events are more important when working on business application, I can not stay only with Item Changed event & writes my whole logic there.
Looking for better reply.
Thanks
First , we register for the items changed event , which captures the moment of creating a connection , after this we bind the target of the connection in two way mode to a property of the ViewModel , called TargetShape. Each time a new connection is created we remove the bindings for the old connections , and we handle the event in this way only if the diagram has already been loaded , because if we handle the event during serialization the targets become "null"s because of the binding. That is my way of managing with connections , I have also made an event "TargetShapeChanged" , through which to do some other operations.
Hope you like it , and use it if do not know other possibilities :)
private void diagram_ItemsChanged(object sender, DiagramItemsChangedEventArgs e)
{
if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
{
if (e.NewItems != null && e.NewItems.Count() > 0)
{
if (e.NewItems.FirstOrDefault() is RadDiagramConnection)
{
if (!isDiagramReloaded)
{
RadDiagramConnection con = (RadDiagramConnection)e.NewItems.FirstOrDefault();
foreach (RadDiagramConnection connection in diagram.Connections)
{
IShape target = (IShape)connection.Target;
connection.ClearValue(RadDiagramConnection.TargetProperty);
connection.Target = target;
}
ViewModel.TargetShape = null;
Binding targetBinding = new Binding("TargetShape");
targetBinding.Source = this.DataContext;
targetBinding.Mode = BindingMode.TwoWay;
con.SetBinding(RadDiagramConnection.TargetProperty, targetBinding);
}
}
}
}
For the official release, we will be exposing two events, which can be handled. They will be raised respectively in the beginning and in the end of each connection manipulation - when creating a connection, when attaching, detaching from shape or when changing the end points of a floating connection. You will be able to handle these events and prevent the action from taking place. Stay tuned for the release.
Greetings,Alex Fidanov
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
Regards
private
void
diagram_ConnectionManipulationCompleted(
object
sender, ManipulationRoutedEventArgs e)
{
var connectedShapes = diagram.GetOutgoingConnectionsForShape(e.Shape).Select(c => c.Target);
var test = connectedShapes.FirstOrDefault().Content;
e.Connection.Target = connectedShapes.FirstOrDefault();
}
Hope it helps.
The event arguments provide some properties to determine the nature of the action that this event describes. For example, you can use the e.ManipulationStatus value to check whether the connections has been attached to a shape, detached from a shape or moved as a floating connection. If you need to check the target shape, you can use the e.Connector property, which represents the connector of the target. The connector itself has a Shape property that you can use to get the target shape reference.
Regards,Alex Fidanov
the Telerik team
Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.
private void diagram_ConnectionManipulationCompleted(object sender, ManipulationRoutedEventArgs e)
{
e.Handled = TestConnection(e.Connection, e.Connector);
}
private bool TestConnection(IConnection connection, IConnector targetConnector)
{
var connector = connection;
var sourceShape = connection.Source;
var sourceConnectorName = connection.SourceConnectorPosition;
var targetShape = targetConnector.Shape;
var targetConnectorName = targetConnector.Name;
return false;
}
I setting up to test if a connection is legal. I have the info I need but how to I cancel the connection? I have tried setting Handled = true and false but the connection is always made. I would like return false from TestConnection and have the connection not occur.
It would be very nice to show an X over the target Connector if it is not a legal connection.
I believe you have in mind the scenario where you create a connection using a shape's connector and try to attach it to another shape. You do can handle the event args of the ConnectionManipulationCompleted event (e.Handled=true). However, please note that this action only determines whether the connection will be attached or not to the target shape. If you check, you would see that the connection is not attached. However, this would not revert the creation of the connection. If you want this as well, I would recommend removing the connection as a whole using the diagram remove methods or using the Undo mechanism.
Greetings,Alex Fidanov
the Telerik team
Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.
As a feature enhancement I would like to see more complete information in the following scenarios:
Connector Mouse Over: Raise a MouseOver Event providing these three properties at the root of the EventArgs:
1) Source Connector (the connector on the other end of the Connection)
2) The Connection itself
3) The Target Cnnector (the connector the Mouse is over)
ConnectionManipulation Completed should allow you to fail the connection and remove it from the diagram without addtional code.
All in all I'm very happy with the Diagram Control, however there are many areas for it to mature in.
Thanks to the team for all their good work!
G
I will wrap up our discussion here as well, in case the someone else encounters the same issues:
First, we in order to provide visual feedback about a connector while it is active/inactive, the RadDiagramConnector.ActivationChangedEvent should be used. However as it doesn't allow you to cancel a connection, we've logged a feature request to provide a preview event to allow you to cancel a connection from attaching to a connector and will provide information about what is activating the connector.
In the meantime you can try the approach demonstrated in the attached solution - the connectors are validated, visual feedback is provided for the invalidated connectors and one connector can only be attached to one connection.
Tina Stancheva
the Telerik team
Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.