camios
camios

Reputation: 342

How to make Hyperlink underline color same as foreground Text color

I would like to have a WPF hyperlink style where the underline is the same color as the text (foreground), where the text foreground color can be overridden with local property and both text and underline colors change to the specified value.

Default styling works (is that because the underline inherits the Pen brush from the Text??), but I need to have my custom style which has different underline thickness. To specify thickness I believe I need to declare its a Pen and its Brush and I don't know how to declare the Brush such that it refers to / inherits from the Style's foreground. Any suggestions please?

<Style TargetType="Hyperlink">
    <Setter Property="Foreground" Value="DarkGrey" />
    <Setter Property="TextDecorations">
        <Setter.Value>
            <TextDecorationCollection>
                <TextDecoration Location="Underline" PenOffset="2">
                    <TextDecoration.Pen>
                        <Pen Brush="[Hyperlink's Foreground of DarkGray]" Thickness="2" />
                    </TextDecoration.Pen>
                </TextDecoration>
            </TextDecorationCollection>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Foreground" Value="Gray" />
            <Setter Property="TextDecorations">
                <Setter.Value>
                    <TextDecorationCollection>
                        <TextDecoration Location="Underline" PenOffset="2">
                            <TextDecoration.Pen>
                                <Pen Brush="[Hyperlink's MouseOver Foreground color of Gray]" Thickness="3" />
                            </TextDecoration.Pen>
                        </TextDecoration>
                    </TextDecorationCollection>
                </Setter.Value>
            </Setter>
        </Trigger>
    </Style.Triggers>
</Style>

Also I can't override the IsMouseOver Trigger Foreground with a local property - am I right that it is due to the limitation of WPF's style triggers and I'm forced to define a new style replacing all trigger(s) just to change that one MouseOver color?

Upvotes: 0

Views: 364

Answers (1)

Clemens
Clemens

Reputation: 128060

You would simply bind the Pen's Brush to the Foreground of the Hyperlink:

<Pen Brush="{Binding Foreground,
             RelativeSource={RelativeSource AncestorType=Hyperlink}}" ... />

Upvotes: 0

Related Questions