2019년 1월 26일 토요일

WPF 프로퍼티와 속성

WPF 프로퍼티와 속성
프로퍼티 개요 및 컨텐트 프로퍼티
n WPF 객체, 엘리먼트들은 프로퍼티를 가지고 있는데 이것에 속성값을 부여해서 요소의 상태를 표현할 수 있다.
n 다음 XAML 코드는 버튼을 표시하고 세 개의 프로퍼티 값을 설정하고 있다.
<Button Foreground="LightSeaGreen"
FontSize="20 pt"
Content="Click Me!"
/>
아래와 같은 “프로퍼티 엘리먼트”로도 표현 가능하다.
<Button FontSize="20 pt"
Content="Click Me!">
<Button.Foreground>
LightSeaGreen
</Button.Foreground>
</Button>
<Button FontSize="20 pt"
ForeGround="LightSeaGreen">
<Button.Content>
Click Me!
</ Button.Content>
</Button>
아래처럼 Button.Content 태그는 생략 가능하다.
<Button FontSize="20 pt"
ForeGround="LightSeaGreen">
Click Me!
</Button>
다음과 같은 표현 역시 가능하다.
<Button>
<Button.Foreground>
LightSeaGreen
</Button.Foreground>
Click Me!
<Button.ForeSize>
20 pt
</Button.ForeSize>
</Button>
컨텐트 프로퍼티는 본 바와 같이 좀 특별하다. XAML에서 ContentControl을 상속한 클래스는 컨텐트 프로퍼티로 인식되는 특별한 하나의 프로퍼티가 있는데 Button의 경우 컨텐트 프로퍼티는 “Content”이다.
컨텐트 프로퍼티로 인식되는 프로퍼티는 System.Window.Serialization 네임스페이스에 정의된 ContentProperty Attribute에 의해 정의된다. PresentationFramework.dll 파일에 Button 클래스는 다음과 같이 정의되어 있다.
[ContentPropery(“Content”)] ß 어트리뷰트 선언
public class Button { }
<Button> 태그 사이의 값을 Button의 Content 프로퍼티에 넣어 준다는 의미임.
StackPanel의 ContentProperty 어트리뷰트는 다음과 같이 정의되어 있다.
[ContentPropery(“Children”)] ß 어트리뷰트 선언
public class StackPanel { }
이 ContentPropery 어트리뷰트는 StackPanel의 자식을 StackPanel 엘리먼트의 자식으로 포함하게 해준다. StackPanel 태그 사이의 요소들을 Children 프로퍼티에 넣어 준다는 이야기이다. 다음 예문을 보자.
<StackPanel Name=”MyStackPanel”>
<Button>Button 1</Button>
<TextBlock>Middle TextBlock</TextBlock>
<Button>Button 2</Button>
</StackPanel>
C# 코드에서 넣으려면 다음과 같이 하면 된다.
stackPanel.Children.Add(new Button
{
Content=”Button 1”
});
……

댓글 없음:

댓글 쓰기

(C#교육동영상)C# ADO.NET 실습 ODP.NET/ODAC 설치 오라클 함수 호출 실습, C#학원, WPF학원, 닷넷학원, 자바학원

  (C#교육동영상)C# ADO.NET 실습  ODP.NET/ODAC 설치  오라클 함수 호출 실습, C#학원, WPF학원, 닷넷학원, 자바학원 https://www.youtube.com/watch?v=qIPU85yAlzc&list=PLxU-i...