views:

37

answers:

1

One of the columns in my DataGrid contains a Hyperlink in a TextBlock. When a row is selected, the hyperlink shows as blue on blue, so i want to change it's text color to white. How can I do that?

The DataGrid looks like this:

<DataGrid>
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="Title">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock TextWrapping="Wrap">
                        <Hyperlink NavigateUri="{Binding Url}">
                            <Run Text="{Binding Title}" />
                        </Hyperlink>
                    </TextBlock>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

I've tried

<Style TargetType="DataGridCell">
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="TextBlock.Foreground" Value="White" />
        </Trigger>
    </Style.Triggers>
</Style>

and the same code with TextElement instead of TextBlock. Both work for other columns, but not for this one with hyperlink.

+1  A: 

Use the following declaration for the link:

<Run Text="{Binding Title}" Foreground="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=DataGridCell},Path=Foreground}"/> 
HCL
This makes the hyperlink the correct color when selected, but also makes it black when not selected and I don't want that.
svick
Extend your style to fit your requirements
HCL
Heh, that didn't occur to me. Yeah it works now, thanks. I'm still curious whether there is a better way to do it, though.
svick