[C#][WPF] XAML 3

LimJaeJun·2024년 3월 19일
0

WPF

목록 보기
4/12

XAML 태그

XAML에서 태그는 반드시 끝맺음 태그를 사용하거나 시작 태그에 슬래시를 사용하여 닫아야한다.

1번 방법

<Button>
</Button>

2번 방법

<Button />

컨트롤러의 속성 변화

HTML은 대소문자를 구분하지 않지만 XAML에서 컨트롤의 이름 및 속성은 .NET Framework의 타입과 연관되어 있어야하기 때문에 구분한다.

1번 방법

<Button>
	<Button.FontWeight>Bold</Button.FontWeight>
    <Button.Content>New Button</Button.Content>
</Button>

2번 방법

<Button FontWeight="Bold"
		Content="New Button">
</Button>

Content 속성에 다른 컨트롤러 포함

Content 속성은 하나의 하위 요소를 가질 수 있다.
예제에서는 WrapPanel을 사용한다.
일단 단순히 컨트롤러들을 담는 컨테이너라고 생각하자.

1번 방법

<Button>
    <Button.FontWeight>Bold</Button.FontWeight>
    <Button.Content>
        <WrapPanel>
            <TextBlock Foreground="Blue">Blue</TextBlock>
            <TextBlock Foreground="Red">Red</TextBlock>
            <TextBlock>Button</TextBlock>
        </WrapPanel>
    </Button.Content>
</Button>

2번 방법

<Button FontWeight="Bold">
    <WrapPanel>
        <TextBlock Foreground="Blue">Blue</TextBlock>
        <TextBlock Foreground="Red">Red</TextBlock>
        <TextBlock>Button</TextBlock>
    </WrapPanel>
</Button>

CodeBehind에서 처리

CodeBehind란 말 그대로 뒤에서 코드로 작성하는 것을 의미한다.
XAML을 사용하지않고 C#코드로 작성할 수 있다.

예시

Button btn = new Button();
btn.FontWeight = FontWeights.Bold;

WrapPanel pnl = new WrapPanel();

TextBlock txt = new TextBlock();
txt.Text = "Multi";
txt.Foreground = Brushes.Blue;
pnl.Children.Add(txt);

txt = new TextBlock();
txt.Text = "Color";
txt.Foreground = Brushes.Red;
pnl.Children.Add(txt);

txt = new TextBlock();
txt.Text = "Button";
pnl.Children.Add(txt);

btn.Content = pnl;
pnlMain.Children.Add(btn);

참고 링크

https://wpf-tutorial.com/ko/6/xaml/xaml-%EA%B8%B0%EB%B3%B8/

profile
Dreams Come True

0개의 댓글