WPF 文本块下划线

发布于 2024-10-31 13:26:43 字数 139 浏览 11 评论 0原文

我有一个宽度文本块,例如500,但我的字符串只是说“H”,但我想下划线 整个 textblock 宽度不只是在 H 下面我该怎么办?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我的黑色迷你裙 2024-11-07 13:26:43

您应该使用 TextBlock 的属性“TextDecorations”。像这样:

 <TextBlock Text="H" TextDecorations="Underline"/>

You should use the property "TextDecorations" of the TextBlock. Like that:

 <TextBlock Text="H" TextDecorations="Underline"/>
勿忘初心 2024-11-07 13:26:43

只是添加我的 2 美分,通过此代码可以在运行时实现与 Talia 的答案相同的效果:

YourTextBlock.TextDecorations = System.Windows.TextDecorations.Underline;

出于某种原因,VS2010 没有显示 RHS 的 Intellisense,但它可以正确编译和运行。

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.

寄居人 2024-11-07 13:26:43
        <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>
        <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>
余厌 2024-11-07 13:26:43

最好的选择可能是使用位于文本块正下方的矩形,其宽度始终是文本块的宽度。像这样:

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文