I’d like to provide you with a SilverBullet™, a small snippet of Silverlight, a class or namespace hidden in the silverlight .NET framework, to help you out in times of need. It’s not to learn, but something to keep in your pocket. Just remember it’s there and you’re safe.
When showing text to a user it’s often necessary to show a few words in Italic or Bold. To do this, use the System.Windows.Documents.Run class. This class provides a small bit of HTML – like features inside a Silverlight TextBlock. But, instead of using HTML tags you have to use the Run class.
- FontWeight – sets the weight of the text, it’s Boldness, and can be of any of the static properties in FontWeights.
- FontStyle – is used to show the text in Italic. The only possibilities are Normal or Italic as provided by FontStyles.
- Foreground – sets the brush for the color of the text. This can be used as any other brush in Silverlight, like an LinearGradientBrush or an ImageBrush
There are couple more properties which you can find here.
Another thing that might become useful when using the Run class to simulate a bit of HTML like behavior is the System.Windows.Documents.LineBreak class. This class does not have any properties or methods and can be compared to the <br /> tag in HTML.
Here’s a small example to show how the Run class is used in xaml:
<Grid x:Name="LayoutRoot" Background="White">
<TextBlock TextWrapping="Wrap" FontFamily="Verdana">
This is an example with
<Run FontStyle="Italic">Italic</Run> and
It's even possible to use<LineBreak />
multiple <LineBreak />lines of text.
Tags van Technorati: Silverlight