Connection with ConnectionLabel object

Feb 22, 2013 at 1:12 PM
Edited Feb 22, 2013 at 1:19 PM
Hi,

I'm trying to set up a BasicConnection label using the ConnectionLabel object, because documentation says it's more flexible. Like this:

// create the connection

BasicConnection connection = new BasicConnection;

connection.PathCalculator = new OrthogonalPathCalculator();
connection.PathRenderer = new LinePathRenderer();
connection.Path = new PathData(PathDataType.Orthogonal);

connection.Source.ConnectedNode = sourceShape;
connection.Source.ConnectorIndex = 1;
connection.SourceDecorator = LineEndDecoratorType.None;

connection.Sink.ConnectedNode = sinkShape;
connection.Sink.ConnectorIndex = 0;
connection.SinkDecorator = LineEndDecoratorType.Arrow;

myDiagram.AddShape(connection);

So far, so good! But I want to get the input of a text box and create a ConnectionLabel for that connection based on that input. The user should type in and click a button to create the label. Here is what I'm doing, inside Click event:

// find the connection in the diagram
SD.Shapes.Templates.Basic.BasicConnection connection = getConnection(myDiagram, shapeID);

// create the label
ConnectionLabel connectionLabel = new ConnectionLabel();
connectionLabel.Position =ConnectionLabelPosition.Center;
connectionLabel.Foreground = new SolidColorBrush(Colors.Blue);
connectionLabel.Background = new SolidColorBrush(Colors.Transparent);
connectionLabel.BorderBrush = new SolidColorBrush(Colors.Black);
connectionLabel.BorderThickness = new Thickness(1);
connectionLabel.Visibility = System.Windows.Visibility.Visible;
connectionLabel.Content = SOME_USER_INPUT;

//now, how to do so that this label shows up? Like this...
connectionLabel.UpdatePosition(connection);

// or like this?
connection.Content = connectionLabel;

When I step into the Loaded event of the connection, it's possible to see this tree:

Connection.Content = ConnectionLabel --> ok
ConnectionLabel.Content = SOME_USER_INPUT -->ok

But the label is never shown. Did I misconcept the usage of ConnectionLabel or am I missing something?

Thanks in advance!
Coordinator
Feb 26, 2013 at 9:19 AM
Olá Kylderi,

ConnectionLabel is intended to be included in the Connection template. Only if found during the OnApplyTemplate event the label will be considered.

Here you have the default connection template for your reference:
            <ControlTemplate TargetType="shapesConnections:Connection">
                <Canvas>
                    <Path x:Name="Path" 
                        Stroke="{TemplateBinding BorderBrush}" 
                        StrokeThickness="{TemplateBinding Thickness}" 
                        StrokeDashArray="{TemplateBinding DashArray}" 
                        StrokeStartLineCap="Round" StrokeEndLineCap="Round" />

                    <Path x:Name="CollisionPath" Stroke="Transparent" />

                    <shapesConnections:ConnectionLabel Padding="5" shapes:Decorator.IsTextDecorator="True">
                        <TextBlock Foreground="{TemplateBinding Foreground}" 
                            Text="{TemplateBinding Text}"
                            TextDecorations="{TemplateBinding TextDecorations}" />
                    </shapesConnections:ConnectionLabel>

                    <shapesConnections:LineEndDecorator x:Name="SourceDecorator" RenderTransformOrigin="0, 0.5"  
                        Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        Thickness="{TemplateBinding Thickness}"
                        Style="{TemplateBinding SourceDecoratorStyle}" />

                    <shapesConnections:LineEndDecorator x:Name="SinkDecorator" RenderTransformOrigin="0, 0.5"  
                        Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}" 
                        Thickness="{TemplateBinding Thickness}"
                        Style="{TemplateBinding SinkDecoratorStyle}" />

                    <ContentPresenter Content="{TemplateBinding Source}" />
                    <ContentPresenter Content="{TemplateBinding Sink}" />
                </Canvas>
            </ControlTemplate>