본문 바로가기
Windows/C#/WPF

Object 요소 property

by hirudev 2021. 5. 1.

오브젝트 요소 프로퍼티의 설정 방법은 크게 2가지로 나뉩니다.

  •  속성 구문
  •  프로퍼티 요소 구문

 

먼저 아래와 같은 클래스가 있다고 가정합니다.

 

namespace testProg{
    using System;
    
    public class Person
    {
    	public Person(){
        	this.Birthday = DateTime.Now;
        }
    }
    public DateTime Birthday { get; private set; }
    
    public string FullName { get; set; }
    public int Salary { get; set; }
}

 

■ 속성 구문

<p:Person xmlns:p="clr-namespace:testProg; assembly=testProg" FullName="홍길동" Salary="300000" />

 

■ 프로퍼티 요소 구문

<p:Person xmlns:p="clr-namespace:testProg; assembly=testProg">
    <p:Person.FullName>
    	홍길동
    </p:Person.FullName>
    <p:Person.Salary>
    	300000
    </p:Person.Salary>
</p:Person>

 

■ 둘의 차이점이라면?

 결과적으로 둘다 같은 결과를 나타냅니다.

위 코드는 단순하여 큰 차이를 느끼지 못하지만 코드가 커질 경우 차이를 느낄 수 있습니다.

예를 들어, Person 클래스 내에 Person 형태의 변수를 가지는 Father, Mother 가 있다고 가정합니다.

코드로 나타내면 아래와 같습니다.

 

namespace testProg{
    using System;
    
    public class Person
    {
    	public Person(){
        	this.Birthday = DateTime.Now;
        }
    }
    public DateTime Birthday { get; private set; }
    
    public string FullName { get; set; }
    public int Salary { get; set; }
    
    // 아랫 부분을 추가합니다.
    public Person Father { get; set; }
    public Person Mother { get; set; }
}

 

위의 경우, '속성 구문' 으로는 값을 넣을 수 없지만, '프로퍼티 요소 구문' 으로는 처리가 가능합니다.

예는 아래의 코드와 같습니다.

 

<p:Person xmlns:p="clr-namespace:testProg; assembly=testProg">
    <p:Person.Father>
    	<p: Person FullName="홍길동 01" />
    </p:Person.Father>
    <p:Person.Mother>
    	<p: Person FullName="홍길동 02" />
    </p:Person.Mother>
</p:Person>

 

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

WPF 에서 WinForm 컨트롤을 생성하는 방법  (0) 2021.05.01
레이아웃 종류  (0) 2021.05.01
XAML 의 로드  (0) 2021.04.30
WPF XAML 스타일 정의 예제  (0) 2021.04.29
WPF 의 구성  (0) 2021.04.28

댓글