如何向行UIElement添加文本?我希望将案文放在行中间。
<Line Stroke="Black" X1="{Binding From.CanvasCenterX}" Y1="{Binding From.CanvasCenterY}" X2="{Binding To.CanvasCenterX}" Y2="{Binding To.CanvasCenterY}" StrokeThickness="2" />
这可能吗?
下面的 XAML 代码将文本添加到行 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>
VerticalAlignment
和HorizontalAlignment
将<TextBlock../>
放在<Grid../>
您需要根据文本长度设置 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" />