Tad
Tad

Reputation: 659

How can I underline a databound value from a DataTemplate?

If I want to display an underlined value in a TextBlock, I have to use a Run element. (If there's a better/easier way, I'd love to hear about it.)

<TextBlock>
  <Run TextDecorations="Underline" Text="MyText" />
</TextBlock>

Ideally, to implement this within a DataTemplate, it would look something like this:

<DataTemplate x:Key="underlineTemplate">
  <TextBlock>
    <Run TextDecorations="Underline" Text="{Binding Value}" />
  </TextBlock>
</DataTemplate>

This won't work, however, because the Run's Text property isn't a DependencyProperty, so you can't databind to it. Does anyone know how I can accomplish this?

Upvotes: 0

Views: 835

Answers (2)

joshperry
joshperry

Reputation: 42227

TextDecoration is an attached property so it can be applied to the TextBlock also. You create some pretty cool effects by templating the TextDecorations property.

See this MSDN article.

<TextBlock TextDecorations="Underline" Text="{Binding Value}" />

Upvotes: 2

Sacha Bruttin
Sacha Bruttin

Reputation: 4153

This works for me:

<TextBlock Text="MyText" TextDecorations="Underline" />

Upvotes: 0

Related Questions