0

How do I add text to a Line UIElement? I would like to have the text placed in the middle of the line.

<Line Stroke="Black" X1="{Binding From.CanvasCenterX}" Y1="{Binding From.CanvasCenterY}" X2="{Binding To.CanvasCenterX}" Y2="{Binding To.CanvasCenterY}" StrokeThickness="2" />

Is this possible?

MikaelKP
  • 133
  • 2
  • 18

2 Answers2

0

The following XAML code adds text to a Line UIElement. In this example the text is presented by a <TextBlock... />. The text is centered at the Line, but this can easily be changed by the TextAlignment property.

<Grid>
    <TextBlock Text="{Binding RelationName}" TextAlignment="Center" VerticalAlignment="Center" HorizontalAlignment="Center"/>
    <Line Stroke="Black" X1="{Binding From.CanvasCenterX}" Y1="{Binding From.CanvasCenterY}" X2="{Binding To.CanvasCenterX}" Y2="{Binding To.CanvasCenterY}" StrokeThickness="2" />
</Grid>

VerticalAlignment and HorizontalAlignment places the <TextBlock../> in the <Grid../>

MikaelKP
  • 133
  • 2
  • 18
0

You need to set X2 coordinate value based on length of Text for alignment.

<Line Stroke="Black" 
    X1="{Binding From.CanvasCenterX}" Y1="{Binding From.CanvasCenterY}"
    X2="{Binding To.CanvasCenterX}" Y2="{Binding To.CanvasCenterY}" StrokeThickness="2" />

<TextBlock Text="Line Between the Text!" 
     VerticalAlignment="Center" HorizontalAlignment="Center"/>

<Line Stroke="Black" 
    X1="{Binding From.CanvasCenterX}" Y1="{Binding From.CanvasCenterY}" 
    X2="{Binding To.CanvasCenterX}" Y2="{Binding To.CanvasCenterY}" StrokeThickness="2" />
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Hemant Patel
  • 184
  • 6