如何在WP8中为不同颜色的文字加下划线?

时间:2014-02-09 19:43:59

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

我有以下代码:

 <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有可能吗?如果是,我的样品出了什么问题?

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

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

2 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:-1)

出于这些目的,我使用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

相关问题