Tuesday, March 8, 2011

TextBlock

Consider the TextBlock object:

TextBlock txt = new TextBlock();
txt.FontSize = 32; // 24 points

txt.Inlines.Add("This is some ");
txt.Inlines.Add(new Italic(new Run("Italic")));
txt.Inlines.Add(" text, and this is some");
txt.Inlines.Add(new Bold(new Run("Bold")));

txt.Inlines.Add(" text, and let's cap it off some ");
txt.Inlines.Add(new Bold(new Italic(new Run("bold italic"))));
txt.Inlines.Add(" text.");
txt.TextWrapping = TextWrapping.Wrap;



The TextBlock class derives directly from FrameworkElement. It defines a property named Inlines, which is of type InlineCollection, which is a collection of Inline objects.

TextBlock itself is included in the System.Windows.Controls namespace but Inline is part of System.Windows.Documents, and it doesn't even derive from UIElement. Here's a partial class hierarchy showing Inline and some of its descendents:

Object
--DispatcherObject (abstract)
------DependencyObject
----------ContentElement
--------------FrameworkContentElement
--------------------TextElement (abstract)
--------------------Inline (abstract)
------------------------------Run
------------------------------Span
--------------------------------------Bold
--------------------------------------Hyperlink
--------------------------------------Italic
--------------------------------------Underline

No comments:

Post a Comment