【问题标题】:how to underline text with different color in WP8?如何在 WP8 中为不同颜色的文本添加下划线?
【发布时间】:2019-01-08 16:13:45
【问题描述】:

我有以下代码:

 <TextBlock HorizontalAlignment="Center" TextWrapping="Wrap" Style="{StaticResource PhoneTextTitle1Style}" FontSize="65">
       <Underline Foreground="DeepSkyBlue">
            <Run Foreground="Turquoise" Text="{Binding SomeProp}"></Run>
       </Underline>
 </TextBlock>

我需要用绿松石色绘制我的文本并使用其他颜色“DeepSkyBlue”在其下划线。我认为Run 元素应该为自己覆盖父控件Foreground 属性,但看起来这是错误的假设(实际上它会覆盖但我需要下划线以保持其他颜色)。 WP8可以吗?如果是,我的样本有什么问题?

编辑:在Pantelis 和aloisdg 的帮助下,工作代码如下所示:

        <Grid Margin="0,0,0,10" HorizontalAlignment="Center">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"></ColumnDefinition>
            </Grid.ColumnDefinitions>

            <Border BorderBrush="DeepSkyBlue" BorderThickness="0,0,0,1">
                <TextBlock TextWrapping="Wrap" Style="{StaticResource PhoneTextTitle1Style}" FontSize="65">
                    <Run Foreground="Turquoise" Text="{Binding SomeProp}"></Run>
                </TextBlock>
            </Border>
        </Grid>

【问题讨论】:

  • 如果没有 100% 的把握,我认为这是不可能的。您可以使用堆栈面板来表示文本块,并使用矩形或边框或任何其他适合这种情况的元素来绘制下划线。
  • 您的回答是正确的。使用 Border + Grid 的方法解决了这个问题。

标签: c# xaml windows-phone-8 windows-phone


【解决方案1】:

我不确定这是不是最好的方法,但您可以使用border。

<Border BorderBrush="DeepSkyBlue" BorderThickness="0,0,0,1">
    <!-- your text -->
</Border>

【讨论】:

    【解决方案2】:

    出于这些目的,我使用 TextDecorations 属性。 例如在 XAML 中:

    <TextBlock Name="undelinedTextBlock" 
               TextDecorations="Underline"
               Text="Underlined text"/>
    

    在 C# 中:

     undelinedTextBlock.TextDecorations = TextDecorations.Underline;
    

    在此处查看详细信息:https://msdn.microsoft.com/en-us/library/system.windows.controls.textblock.textdecorations(v=vs.105).aspx

    【讨论】:

    • 请注意,该解决方案也适合 wp8.1 silverlight
    猜你喜欢
    • 1970-01-01
    • 2012-08-17
    • 2013-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-05
    • 2012-04-18
    • 2011-04-13
    相关资源
    最近更新 更多