简体   繁体   中英

RichTextBox Hyperlink color in Windows Phone

I have an application that displays some text in a RichTextBox; I have some links in the text, so use Hyperlinks to display them.

My problem is that I can't get to set a color to the Hyperlink Foreground property. I know it can't pass it via RichTextBox.Xaml property; moreover, If i do

<RichTextBox.Resources>
    <Style TargetType="Hyperlink" x:Key="HyperlinkColor">
        <Setter Property="Foreground" Value="Black"/>
    </Style>
</RichTextBox.Resources>

nothing happens: the hyperlink remains stuck with the default color.

Is there a way to get the color I want on Hyperlinks in my RichTextBox?

You can do:

        <RichTextBox>

            <RichTextBox.Resources>
                <SolidColorBrush x:Key="HyperlinkColorBrush"
                                 Color="White" />
            </RichTextBox.Resources>

            <Paragraph>
                <Hyperlink Foreground="{StaticResource HyperlinkColorBrush}">
                    Hi!
                </Hyperlink>
            </Paragraph>
            <Paragraph>
                <Hyperlink Foreground="{StaticResource HyperlinkColorBrush}">
                    How are you?
                </Hyperlink>
            </Paragraph>

        </RichTextBox>

OR

cs:

        string xaml =
            @"<Section xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"">
                <Paragraph>
                    <Hyperlink Foreground=""Green"">Hyperlink</Hyperlink>
                </Paragraph>
            </Section>";

        RichTextBox.Xaml = xaml;

I hope this helps :)

I just set the foreground property like this:

<Hyperlink NavigateUri="http://www.****.org" TargetName="_blank" Foreground="#FF337CBB">www.***.org</Hyperlink>

and it worked out for me.

Remove the x:Key attribute from Style, so that it gets applied automatically to all hyperlinks which exists under RichTextBox.

<Style TargetType="Hyperlink">
   <Setter Property="Foreground" Value="Black"/>
</Style>

I've faced the same problem! And here's the solution that works:

<SolidColorBrush x:Key="HyperlinkForegroundThemeBrush"
                             Color="TheColorYouNeed" />
<SolidColorBrush x:Key="HyperlinkPressedForegroundThemeBrush"
                             Color="TheColorYouNeed"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM