Reputation: 917
I have a textblock
of width
say 500
, but my string is just say "H" but I want to underline
the whole textblock
width not just under H what can I do?
Upvotes: 73
Views: 82841
Reputation: 11787
<TextBlock VerticalAlignment="Bottom"
HorizontalAlignment="Center"
Margin="40"
Height="40"
FontSize="16"
Tapped="TextBlock_Tapped"
Text="Text"
Foreground="{StaticResource LightBlue}">
<Underline>
<Run Text="Text"/>
</Underline>
</TextBlock>
Upvotes: 15
Reputation: 35450
Just to add my 2 cents, The same effect as Talia's answer can be achieved at runtime through this code:
YourTextBlock.TextDecorations = System.Windows.TextDecorations.Underline;
For some reason VS2010 doesn't show Intellisense for the RHS, but it compiles and runs correctly.
Upvotes: 25
Reputation: 104
Your best bet would probably be to use a Rectangle positioned immediately below the text block, whose width is always the width of the text block. Like this:
<DockPanel LastChildFill="False">
<TextBlock DockPanel.Dock="Top" x:Name="blockToUnderline" Text="H" Width="76" />
<Rectangle DockPanel.Dock="Top" Fill="Black" Height=1 Width="{Binding ElementName=blockToUnderline, Path=ActualWidth}" />
</DockPanel>
Upvotes: 1
Reputation: 2295
You should use the property "TextDecorations" of the TextBlock. Like that:
<TextBlock Text="H" TextDecorations="Underline"/>
Upvotes: 227