■ TextBlock 클래스의 TextDecorations 속성을 사용하는 방법을 보여준다.
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" Height="600" Title="TextBlock 클래스 : TextDecorations 속성 사용하기" FontFamily="나눔고딕코딩" FontSize="16"> <Window.Resources> <Style TargetType="TextBlock"> <Setter Property="Margin" Value="20" /> <Setter Property="FontSize" Value="32" /> <Setter Property="Text" Value="The quick red fox" /> </Style> </Window.Resources> <Grid> <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"> <TextBlock Name="textBlock1" /> <TextBlock Name="textBlock2" /> <TextBlock Name="textBlock3" /> <TextBlock Name="textBlock4" /> <TextBlock Name="textBlock5" /> </StackPanel> </Grid> </Window> |
▶ MainWindow.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
using System.Windows; using System.Windows.Controls; using System.Windows.Media; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); Loaded += Window_Loaded; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 윈도우 로드시 처리하기 - Window_Loaded(sender, e) /// <summary> /// 윈도우 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Window_Loaded(object sender, RoutedEventArgs e) { SetTextDecoration1(this.textBlock1); SetTextDecoration2(this.textBlock2); SetTextDecoration3(this.textBlock3); SetTextDecoration4(this.textBlock4); SetTextDecoration5(this.textBlock5); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 텍스트 데코레이션 설정하기 1 - SetTextDecoration1(textBlock) /// <summary> /// 텍스트 데코레이션 설정하기 1 /// </summary> /// <param name="textBlock">텍스트 블럭</param> private void SetTextDecoration1(TextBlock textBlock) { TextDecoration decoration = new TextDecoration ( TextDecorationLocation.Baseline, new Pen(Brushes.Maroon, 1), 2.0, TextDecorationUnit.Pixel, TextDecorationUnit.Pixel ); TextDecorationCollection collection = new TextDecorationCollection(); collection.Add(decoration); textBlock.TextDecorations = collection; } #endregion #region 텍스트 데코레이션 설정하기 2 - SetTextDecoration2(textBlock) /// <summary> /// 텍스트 데코레이션 설정하기 2 /// </summary> /// <param name="textBlock">텍스트 블럭</param> private void SetTextDecoration2(TextBlock textBlock) { TextDecoration decoration = new TextDecoration(); decoration.Pen = new Pen(Brushes.Red, 1); decoration.PenThicknessUnit = TextDecorationUnit.FontRecommended; TextDecorationCollection collection = new TextDecorationCollection(); collection.Add(decoration); textBlock.TextDecorations = collection; } #endregion #region 텍스트 데코레이션 설정하기 3 - SetTextDecoration3(textBlock) /// <summary> /// 텍스트 데코레이션 설정하기 3 /// </summary> /// <param name="textBlock">텍스트 블럭</param> private void SetTextDecoration3(TextBlock textBlock) { TextDecoration decoration = new TextDecoration(); decoration.Location = TextDecorationLocation.Underline; decoration.Pen = new Pen(Brushes.Red, 1); decoration.PenOffset = 2; decoration.PenOffsetUnit = TextDecorationUnit.Pixel; decoration.PenThicknessUnit = TextDecorationUnit.Pixel; TextDecorationCollection collection = new TextDecorationCollection(); collection.Add(decoration); textBlock.TextDecorations = collection; } #endregion #region 텍스트 데코레이션 설정하기 4 - SetTextDecoration4(textBlock) /// <summary> /// 텍스트 데코레이션 설정하기 4 /// </summary> /// <param name="textBlock">텍스트 블럭</param> private void SetTextDecoration4(TextBlock textBlock) { Pen pen = new Pen(); pen.Thickness = 1.5; pen.DashStyle = DashStyles.Dash; pen.Brush = new LinearGradientBrush ( Colors.Yellow, Colors.Red, new Point(0, 0.5), new Point(1, 0.5) ); pen.Brush.Opacity = 0.5; TextDecoration decoration = new TextDecoration(); decoration.Pen = pen; decoration.PenThicknessUnit = TextDecorationUnit.FontRecommended; TextDecorationCollection collection = new TextDecorationCollection(); collection.Add(decoration); textBlock.TextDecorations = collection; } #endregion #region 텍스트 데코레이션 설정하기 5 - SetTextDecoration5(textBlock) /// <summary> /// 텍스트 데코레이션 설정하기 5 /// </summary> /// <param name="textBlock">텍스트 블럭</param> private void SetTextDecoration5(TextBlock textBlock) { textBlock.TextDecorations = TextDecorations.Strikethrough; } #endregion } } |