Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Styling Hyperlink inside a TextBlock

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.

like image 574
svick Avatar asked Aug 03 '10 20:08

svick


1 Answers

Use the following declaration for the link:

<Run Text="{Binding Title}" 
     Foreground="{Binding 
         RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGridCell},
                                        Path=Foreground}"/> 
like image 68
HCL Avatar answered Oct 02 '22 21:10

HCL