tags:

views:

892

answers:

1

I'm writing a WPF app, and I want some of my hyperlinks to be the default blue, and others to be green. No problem -- I can just set Foreground:

<TextBlock><Hyperlink Foreground="#0C0">Mark as done</Hyperlink></TextBlock>

The trouble is, when I do this, the hover effect goes away. In a normal Hyperlink, when I move the mouse over the link, it turns red, and when I move the mouse away, it goes back to blue. But in a Hyperlink with the Foreground property assigned, it's always that color -- the red-on-hover is totally suppressed.

How can I change a Hyperlink's color, without losing the default hover behavior and color?

+4  A: 

Setting the Foreground directly (as you've done) doesn't work, and setting it in a Style doesn't work either, unless you "derive" that style from the default Hyperlink style (which must include the OnMouseOver trigger). So this works:

<TextBlock>
    <Hyperlink>
        <Hyperlink.Style>
            <Style TargetType="Hyperlink"
                   BasedOn="{StaticResource {x:Type Hyperlink}}">
                <Setter Property="Foreground" Value="#0C0"/>
            </Style>
        </Hyperlink.Style>
        Mark as done
    </Hyperlink>
</TextBlock>

Extracting that style back into the Window resources and referencing it with a key would probably make for more-readable XAML, but the above code does the job.

Matt Hamilton
Indeed it does -- I had tried something similar, but was missing the BasedOn. Thanks!
Joe White
lol I just found this answer on Google, trying to solve the same problem. I'd vote myself up if I could! :)
Matt Hamilton