이 Post 에서는 WPF 의 TextBlock 에서 일부 글자에만 Bold, Italic, Underline 혹은 글자색, 배경색 등의 서식을 적용하는 방법을 알아 봅니다.
0. 실행 화면

1. MainWindow.xaml
<Window x:Class="TextBlockStyleTest00.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TextBlockStyleTest00"
mc:Ignorable="d"
Title="MainWindow" Height="300" Width="600">
<Grid>
<TextBlock x:Name="tbMain" TextWrapping="Wrap" FontSize="32"/>
</Grid>
</Window>
10: 단순히 tbMain 이라는 텍스트 블럭을 하나 추가해 줍니다.
1. MainWindow.xaml.cs
public MainWindow()
{
InitializeComponent();
tbMain.Inlines.Clear();
tbMain.Inlines.Add(new Run("This is a test of the "));
tbMain.Inlines.Add(new Run("TextBlock") { FontWeight = FontWeights.Bold, FontStyle = FontStyles.Italic, Foreground = Brushes.Red });
tbMain.Inlines.Add(new Run(" style.\n"));
tbMain.Inlines.Add(new Run("\n"));
tbMain.Inlines.Add(new Run("This is a test of the "));
tbMain.Inlines.Add(new Span(new Run("TextBlock") { Foreground = Brushes.Red, TextDecorations = TextDecorations.Underline }) { Background = Brushes.Yellow });
tbMain.Inlines.Add(new Run(" style.\n"));
}
8: Run 을 사용해서 각종 서식을 지정하고 있습니다. Foreground 를 사용해 글자 전경색을 설정 합니다.
14: 사실 Run 에서는 배경색 설정하는 부분이 없습니다. 배경색은 Span 으로 설정 해주시면 됩니다.