How to Change Connector Style Based on its SourcePort in WPF Diagram?
In the WPF Diagram, you can change the connector style based on the style of its SourcePort by retrieving the fill color of the source port to which the connector is connected. Then, apply this color to the stroke of the connector line when the connector target drag is completed by handling the ConnectorTargetChangedEvent. We have provided the code snippet to achieve this.
Code Snippet:
private void MainWindow_ConnectorTargetChangedEvent(object sender, ChangeEventArgs<object, ConnectorChangedEventArgs> args)
{
if (args.NewValue.DragState == DragState.Completed)
{
Style style = new Style();
//obtaint the connector
var connector = args.Item as ConnectorViewModel;
if (connector.SourcePort != null)
{
Brush fillColor = new SolidColorBrush(Colors.CornflowerBlue);
foreach (Setter setter in connector.SourcePort.ShapeStyle.Setters)
{
if (setter.Property.Name == "Fill")
{
//obtaint fill color of the port
fillColor = (Brush)setter.Value;
break;
}
}
foreach (Setter setter in connector.ConnectorGeometryStyle.Setters)
{
if (setter.Property.Name == "Stroke")
//setting fill color of the port to connector line color
style.Setters.Add(new Setter() { Property = Shape.StrokeProperty, Value = fillColor });
else
style.Setters.Add(setter);
}
//Updating the style to the connector geometry.
connector.ConnectorGeometryStyle = style;
}
}
}
Conclusion
I hope you enjoyed learning about how to change the connector style based on its SourcePort in WPF Diagram.
You can refer to our WPF Diagram feature tour page to learn about its other groundbreaking feature representations and documentation, You can also explore our WPF Diagram example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!