Category / Section
How to restrict node’s dragging from native lane to other lanes in WPF diagram(SfDiagram)?
2 mins read
WPF Diagram (SfDiagram) supports to restricting node’s dragging from the native lane to other lanes. You can achieve this requirement by using the BoundaryConstraints property of NodeChangedCommand. The BoundaryConstraints property is used to restrict the dragging of the nodes in the given custom region. While the node is getting dragged, its parent lane bounds are passed to the BoundaryConstraints property. In the following code sample, CustomNodeVM class has the ParentLane property, which represents the node’s parent lane. The value for ParentLane will be set once the node is added to the diagram by using ItemAddedCommand. By using the ParentLane property, lane bounds will passed to the BoundaryConstraints property.
XAML
<syncfusion:SfDiagram x:Name="diagram" ItemAddedCommand="{Binding ItemAddedCommand}" Swimlanes="{Binding Swimlanes}" Nodes="{Binding Nodes}" NodeChangedCommand="{Binding NodeChangedCommand}" Connectors="{Binding Connectors}"/>
C#
//Adding ItemAdded and NodeChanged commands to diagram. ItemAddedCommand = new DelegateCommand(OnItemAdded); NodeChangedCommand = new DelegateCommand(OnNodeChangedCommand); //Method to execute ItemAddedCommand. private void OnItemAdded(object obj) { var args = obj as ItemAddedEventArgs; if (args.OriginalSource != null && args.OriginalSource is LaneViewModel) { if (args.Item != null && args.Item is CustomNodeVM) { //Setting parent lane value to Nodes. (args.Item as CustomNodeVM).ParentLane = args.OriginalSource as LaneViewModel; } } } //Method to execute NodeChangedCommand. private void OnNodeChangedCommand(object obj) { var args = obj as ChangeEventArgs<object, NodeChangedEventArgs>; CustomNodeVM node = args.Item as CustomNodeVM; if (node != null && node.ParentLane != null && args.NewValue.InteractionState == NodeChangedInteractionState.Dragging) { LaneViewModel parentlane = (args.Item as CustomNodeVM).ParentLane as LaneViewModel; //Setting Node’s parent lane bounds region value to BoundaryConstraint property. args.BoundaryConstraints = (parentlane.Info as ILaneInfo).Bounds; } } /// <summary> /// Represents a class to add custom properties to add parent lane property to the node. /// </summary> public class CustomNodeVM : NodeViewModel { private LaneViewModel parentLane = null; /// <summary> /// Gets or sets the Parent Lane of the Node. /// </summary> public LaneViewModel ParentLane { get { return parentLane; } set { if (parentLane != value) { parentLane = value; OnPropertyChanged("ParentLane"); } } } }