前台:默认即可
<Window x:Class="FormatText.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="FormatText" Height="300" Width="300" Loaded="Window_Loaded">
<Grid>
</Grid>
</Window>
后台:Window_Loaded(object sender,RoutedEventArgs e)事件中代码
Title = "Format the Text";
TextBlock txt = new TextBlock();
txt.FontSize = 24; // 24
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 with some ");
txt.Inlines.Add(new Bold(new Italic(new Run("bold italic"))));
txt.Inlines.Add(" text.");
txt.TextWrapping = TextWrapping.Wrap;
this.Content = txt;
TextBlock 类包含在System.Windows.Controls 命名空间内,但Inlines属于System.Windows.Documents
命名空间。具体的继承关系如下:
Object
DispatcherObject (abstract)
DependencyObject
ContentElement
FrameworkContentElement
TextElement (abstract)
Inline (abstract)
Run
Span
Bold
Hyperlink
Italic
Underline