Setting Hyperlink's Foreground

Jan 13, 2009 at 7:53 PM
I'm using the DataGrid's default blue color to highlight the current row, but my hyperlinks are a bit difficult to read since they are blue as well (although light blue).  I'd like to reverse the hyperlink's foreground color from light blue to white for the currently selected row, but can't quite get the XAML right.

I'm trying something like this:

<dg:DataGridHyperlinkColumn x:Name="linkDocNum" Width="Auto" Header="Document #" Binding="{Binding Path=DocumentNum}"> 
        <Style TargetType="TextBlock"> 
            <EventSetter Event="Hyperlink.Click" Handler="DocumentNum_Click" /> 
                <Trigger Property="dg:DataGridRow.IsSelected" Value="True"> 
                    <Setter Property="Foreground" Value="White"/> 

The markup compiles, and I don't get a runtime error, but the foreground color of the hyperlink column doesn't change to white when the row is selected.

Any help is appreciated.



Jan 14, 2009 at 7:09 AM
I haven't looked at the source, or even used the DataGridHyperlinkColumn, but are you sure that you should restyle TextBlock? My guess would be that you want to set the style's TargetType to Hyperlink instead - have you tried that?
Jan 16, 2009 at 8:27 PM
Thanks for the reply, but not sure how / if I can do that.

Are there any other approaches I can take for this, or anybody have an example for the above suggestion?


Feb 3, 2009 at 6:41 PM
Sorry to bring this up again, but I'm hoping someone can help me out with this.

Any takers?

Mar 30, 2009 at 5:48 PM

Well I added a style for the Hyperlink type, and that at least changed the color of the text displayed by DataGridHyperlinkColumn:


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


But I can't seem to get the text to change color when the row is selected. I know I need some sort of a trigger, but what do I tie it to?


Mar 30, 2009 at 8:41 PM
Does something like this not work for you:


<Style TargetType="{x:Type Hyperlink}">
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Foreground" Value="DarkSlateBlue" />
    <Setter Property="Foreground" Value="LightGreen" />


Mar 30, 2009 at 8:47 PM
That does work, but only when the mouse is over the hyperlink itself.  But I'd still like the text color to change when the grid row is selected; i.e. the user will not necessarily move the mouse over the hyperlink just yet.
Apr 15, 2010 at 12:34 PM

I've just come across this thread because I was trying to do the same thing. This works for me:

<Style x:Key="DataGridHyperlinkStyle" TargetType="{x:Type Hyperlink}">
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Custom:DataGridRow}}, Path=IsSelected}" Value="true">
            <Setter Property="Foreground" Value="White"/>
Apr 16, 2010 at 2:16 PM

That did the trick!  Brilliant!!  :-)

A data trigger makes perfect sense.  Thank you very much!!