SilverBullet #10 System.Windows.Documents.Run
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.
The Run class inherits from the abstract System.Windows.Documents.Inline which provides most of the properties to use. A few of the most common are:
- 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 FontWeight="Black">Bold</Run>,
<Run FontStyle="Italic">Italic</Run> and
<Run Foreground="red">Colored</Run>
text...<LineBreak />
It's even possible to use<LineBreak />
multiple <LineBreak />lines of text.
</TextBlock>
</Grid>
|
Tags van Technorati: Silverlight
|
![]() |
|
|


