Setting Hyperlink's Foreground

Jan 13, 2009 at 6: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}"> 
    <dg:DataGridHyperlinkColumn.ElementStyle
        <Style TargetType="TextBlock"> 
            <EventSetter Event="Hyperlink.Click" Handler="DocumentNum_Click" /> 
            <Style.Triggers
                <Trigger Property="dg:DataGridRow.IsSelected" Value="True"> 
                    <Setter Property="Foreground" Value="White"/> 
                </Trigger
            </Style.Triggers
        </Style
    </dg:DataGridHyperlinkColumn.ElementStyle>
</dg:DataGridHyperlinkColumn>

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.

Thanks!
-Larry

 

Jan 14, 2009 at 6: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 7: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?

-Larry


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

Any takers?

-Larry
Mar 30, 2009 at 4: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" />
</
Style>

 

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?

-Larry

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

 

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

 

Mar 30, 2009 at 7: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 11:34 AM

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}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Custom:DataGridRow}}, Path=IsSelected}" Value="true">
            <Setter Property="Foreground" Value="White"/>
        </DataTrigger>
    </Style.Triggers>
</Style>
 
Apr 16, 2010 at 1:16 PM

That did the trick!  Brilliant!!  :-)

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