tags:

views:

487

answers:

1

How do I apply effects such as a DropShadowEffect class to a TextBlock Run element in WPF?

Think of it as a way of highlighting certain areas of text in a TextBlock where the Run element is located, but applying an individual effect to that area instead.

+2  A: 

There are lots of Inline elements that you can use in place of Run, if you want an effect like underline, italics, or strike-through. However, if you want to apply an actual Effect, the element you are applying it to needs to derive from UIElement. Run and the other inline elements do not, but you can nest TextBlocks to be able to apply shader Effects like the DropShadow you are looking for.

<TextBlock>
 <Run Text="This"  />
 <Italic>
  <Run Text="has a" /> 
 </Italic>
 <TextBlock Text="Drop Shadow">
  <TextBlock.Effect>
   <DropShadowEffect />
  </TextBlock.Effect>
 </TextBlock> 
</TextBlock>
rmoore