Dominic K
Dominic K

Reputation: 7075

Drawing text on/on top of line in WPF

I was wondering if it was possible to do either in WPF:

Text on line, text on top of line

I guess the main problem here that I can't embed a textblock in a line in XAML, which is something I'm use to doing. Does anyone have any idea of how I can tackle this problem?

EDIT: It would also have to handle diagonal text.

Upvotes: 2

Views: 3739

Answers (3)

AQuirky
AQuirky

Reputation: 5236

I am adding this answer because I found the accepted answer and other answers did not address the first example, with variable length horizontal lines on both sides of Hello. Here is how to do that...

<Grid>
    <Grid.ColumnDefinitions>
         <ColumnDefinition Width="1*"/>
         <ColumnDefinition Width="Auto"/>
         <ColumnDefinition Width="1*"/>
    </Grid.ColumnDefinitions>
    <Separator Grid.Column="0" Margin="5"/>
    <TextBlock Text="Hello" Grid.Column="1"/>
    <Separator Grid.Column="2" Margin="5"/>
</Grid>

Upvotes: 0

Muad&#39;Dib
Muad&#39;Dib

Reputation: 29216

You can do this, this is actually pretty easy. You have to keep in mind that you can nest content inside a <TextBlock> tag....

<TextBlock>
    <Line X1="0" Y1="0" X2="100" Y2="0" Stroke="Black" StrokeThickness="4"/>
    <TextBlock Text="Hello there!" VerticalAlignment="Center" HorizontalAlignment="Center"/>
    <Line X1="0" Y1="0" X2="100" Y2="0" Stroke="Black" StrokeThickness="4"/>
</TextBlock>

Upvotes: 4

Timwi
Timwi

Reputation: 66573

Could you have a three-column grid, with a line in the first and third column and the text in the second? Of course you’d have to set the left and right line’s properties so that they stretch across the entire width.

Upvotes: 1

Related Questions