티스토리 뷰

C#/WPF

WPF tutorial - The TextBlock control

광그로 2017. 6. 20. 17:47

TextBlock control http://www.wpf-tutorial.com/basic-controls/the-textblock-control/


TextBlock은  Label 컨트롤과 마찬가지로 화면에 텍스트를 넣을 수 있지만 간단하고 리소스가 덜 필요합니다.

TextBlock은 여러 줄, 텍스트 (문자열) 만 포함 할 수있는 반면, Label은 짧고 한 줄짜리 텍스트 (예 : 이미지 포함) 용입니다

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<Window x:Class="WPF_tutorial.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:WPF_tutorial"
        xmlns:system="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="150" Width="350">
 
    <Grid>
        <TextBlock>This is a TextBlock</TextBlock>
    </Grid>
 
</Window>
 
cs



1
2
3
<Grid>
        <TextBlock Margin="10">This is a TextBlock control and it comes with a very long text</TextBlock>
TextBlock의 영역에 10정도의 여백을 부여해라.
 </Grid>
cs


1
2
3
4
5
6
7
8
9
10
11
12
<StackPanel>
        <TextBlock Margin="10" Foreground="Red">
                        This is a TextBlock control<LineBreak />
<LineBreak/m> : C에서의 "\n"과 같은 개행문자의 역할
                        with multiple lines of text.
        </TextBlock>
        <TextBlock Margin="10" TextTrimming="CharacterEllipsis" Foreground="Green">
TextTrimming="CharacterEllipsis" : 텍스트를 맞출 수 없을 때 줄임표로 표시한다.
        CharacterEllipsis 대신, WordEllipsis 사용 가능                
This is a TextBlock control with text that may not be rendered completely, which will be indicated with an ellipsis.
        </TextBlock>
        <TextBlock Margin="10" TextWrapping="Wrap" Foreground="Blue">
TextWrapping="Wrap" : 자동으로 텍스트를 줄바꿈시켜준다.
                        This is a TextBlock control with automatically wrapped text, using the TextWrapping property.
        </TextBlock>
    </StackPanel>
cs



굵게, 기울임, 밑줄

TextBox control은 인라인 서식을 지원합니다.


1
2
3
4
<TextBlock Margin="10" TextWrapping="Wrap">
        TextBlock with <Bold>bold</Bold>, <Italic>italic</Italic> and 
        <Underline>underlined</Underline> text.
    </TextBlock>


cs


Bold, Italic, Underline 이 3가지의 tag들은 모두 Span elemnt의 하위 클래스입니다.

효과를 주기 위하여 Span element의 특정 속성을 설정합니다.



하이퍼링크

하이퍼 링크 요소를 사용하면 텍스트에 링크를 포함 할 수 있습니다.
NavigateUri 속성을 사용하여 이동할 URL을 정의할 수 있습니다.
1
2
3
4
5
<TextBlock Margin="10" TextWrapping="Wrap">
        This text has a <Hyperlink RequestNavigate="Hyperlink_RequestNavigate"
                                   NavigateUri="https://rhkdrmfh.tistory.com">rhkdrmfh</Hyperlink> in it.
    </TextBlock>
 
cs

일반 WPF 응용 프로그램에서 외부 URL을 시작하려면 이벤트와 Process 클래스에서 약간의 도움이 필요합니다.
1
2
3
4
private void Hyperlink_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
{
        System.Diagnostics.Process.Start(e.Uri.AbsoluteUri);
}
cs



Span

Span 요소에는 기본적으로 특정 렌더링이 없지만 글꼴 크기, 스타일 및 두께, 배경 및 색 등 거의 모든 종류의 특정 렌더링을 설정할 수 있습니다.

Span 요소의 가장 큰 장점은 내부에 다른 인라인 요소를 허용하여 텍스트와 스타일의 고급 조합을 쉽게 수행 할 수 있다는 것입니다.


1
2
3
4
5
6
7
8
9
10
<TextBlock Margin="10" TextWrapping="Wrap">
                        This <Span FontWeight="Bold">is</Span> a
                        <Span Background="Silver" Foreground="Maroon">TextBlock</Span>
                        with <Span TextDecorations="Underline">several</Span>
                        <Span FontStyle="Italic">Span</Span> elements,
        
                        <Span Foreground="Blue">
                                using a <Bold>variety</Bold> of <Italic>styles</Italic>
                        </Span>.
    </TextBlock>
cs


FontWeight(굵기), Background(배경색), Foreground(글자색), TextDecorations(선/밑줄,취소선 등), FontStyle(글자체)


Code-behind에서도 span을 적용할 수 있습니다. 

하지만 귀찮은 점이 많습니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public MainWindow()
        {
            InitializeComponent();
            TextBlock tb = new TextBlock();
            tb.TextWrapping = TextWrapping.Wrap;
            tb.Margin = new Thickness(10);
            tb.Inlines.Add("An example on ");
            tb.Inlines.Add(new Run("the TextBlock control ") { FontWeight = FontWeights.Bold });
            tb.Inlines.Add("using ");
            tb.Inlines.Add(new Run("inline ") { FontStyle = FontStyles.Italic });
            tb.Inlines.Add(new Run("text formatting ") { Foreground = Brushes.Blue });
            tb.Inlines.Add("from ");
            tb.Inlines.Add(new Run("Code-Behind") { TextDecorations = TextDecorations.Underline });
            tb.Inlines.Add(".");
            this.Content = tb;
        }  
cs





'C# > WPF' 카테고리의 다른 글

WPF tutorial - The TextBox control  (0) 2017.06.20
WPF tutorial - The Label control  (0) 2017.06.20
WPF tutorial - Handling exceptions in WPF  (0) 2017.06.20
WPF tutorial - Resource  (0) 2017.06.20
WPF tutorial - Command-line parameters in WPF  (2) 2017.06.20
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/02   »
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
글 보관함