Run Class in Silverlight

Hello All,


While using Silverlight most of you must have come across a situation wherein you have to show the text like this:


This is my text.


One question comes into mind is "How to do this in Silverlight" as we can do this in HTML using span tag.


The Run class which is inherited from the abstract System.Windows.Documents.Inline.


Run Class has following Properties:
  1. FontFamily
  2. FontSize
  3. FontStyle
  4. FontWeight
  5. TextDecorations
and some more.


In Xaml code it was very easy to do it.


Example:


<TextBlock x:Name="tb">
     <Run FontWeight="Bold">This</Run>
     <Run Foreground="Blue">is my</Run>
     <Run ForeStyle="Italic">Text.</Run>
</TextBlock>


Output:


This is my Text.


Similarly, we can do it in code behind:


tb.Inlines.Add(new Run() { Text="This ", FontSize=12, FontWeight = FontWeights.Bold });
tb.Inlines.Add(new Run() { Text="is my ", FontSize=12, Foreground = new SolidColorBrush(Colors.Blue});
tb.Inlines.Add(new Run() { Text="Text.", FontSize=12, FontStyle = FontStyles.Italic });


and thats all you have to do.


Happy coding :)