将文本添加到WPF行

时间:2014-12-02 14:27:16

标签: wpf text line uielement

如何向Line UIElement添加文字?我想把文字放在行的中间。

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

这可能吗?

2 个答案:

答案 0 :(得分:0)

以下XAML代码将文本添加到Line UIElement。在此示例中,文本由<TextBlock... />呈现。文本以Line为中心,但TextAlignment属性可以轻松更改。

<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>

VerticalAlignmentHorizontalAlignment<TextBlock../>放入<Grid../>

答案 1 :(得分:0)

您需要根据文本的长度设置X2坐标值以进行对齐。

<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" />