Tim Taurit
Tim Taurit

Reputation: 9239

WPF - style hyperlinks in DataGrids

I need to style hyperlinks in WPF4 DataGrid control (they appear in columns of type DataGridHyperlinkColumn). I have many DataGrids in the project and would like to apply the hyperlink style to all of them.

I found this Q&A: WPF Style DataGridHyperlinkColumn and created the style for HyperLink control:

<Style TargetType="{x:Type Hyperlink}">
    <Setter Property="TextDecorations" Value="" />
</Style>

It works fine, but obviously it also affects all other hyperlinks, eg. in

<TextBlock>
    <Hyperlink NavigateUri="http://www.google.co.in">Click here</Hyperlink>
</TextBlock>

How can I target only hyperlinks in DataGrids? In CSS syntax it would be something like

DataGrid Hyperlink {TextDecorations: ""; }

Upvotes: 4

Views: 1454

Answers (1)

Allan Chua
Allan Chua

Reputation: 10175

Due to property value inheritance all instances of links inherits the style that you have created because you did not use x:key attribute.

You can add x:Key attribute:

<Style TargetType="{x:Type Hyperlink}" x:Key="HyperlinkStyle1">
    <Setter Property="TextDecorations" Value="" />
</Style>

by using this you can reference this from your controls like below:

<Hyperlink NavigateUri="http://www.google.co.in" Style={StaticResource HyperlinkStyle1}>Click here</Hyperlink>

Upvotes: 3

Related Questions