在TextBlock中设置超链接样式

时间:2010-08-03 20:48:47

标签: wpf xaml datagrid styles

DataGrid中的一列包含Hyperlink中的TextBlock。选择行时,超链接在蓝色上显示为蓝色,因此我想将其文本颜色更改为白色。我怎么能这样做?

DataGrid看起来像这样:

<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>

我试过

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

和代码TextElement代替TextBlock的代码相同。两者都适用于其他列,但不适用于具有超链接的列。

1 个答案:

答案 0 :(得分:2)

对链接使用以下声明:

<Run Text="{Binding Title}" 
     Foreground="{Binding 
         RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGridCell},
                                        Path=Foreground}"/> 
相关问题