코드 비하인드 단에서 클래스를 정의하고 아래와 같이 값을 초기화 했습니다.
//MainWindow.xaml.cs
![[c# wpf] xaml에서 class 초기값 설정 방법 기존 초기화방법](http://t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png)
그리고 초기값을 xaml상에서 출력하기 위해 DataContext에 해당 클래스를 할당한 후 바인딩을 통해 출력을 했습니다.
//MainWindow.xaml
![[c# wpf] xaml에서 class 초기값 설정 방법 기존 datacontext 방법](http://t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png)
하지만 우리가 이번 포스팅에서 다룰 것은 코드 비하인드 단이 아닌 xaml상에서 클래스 초기값을 설정하는 방법입니다.
이 글을 읽기 전 선수 지식 포스팅
1. 먼저 기존에 작성했던 초기 값을 지우겠습니다.
![[c# wpf] xaml에서 class 초기값 설정 방법 - 이 글을 읽기 전 선수 지식 포스팅 초기값 지우기](http://t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png)
2. <Window.Datacontext/> 태그를 지워줍니다.
![[c# wpf] xaml에서 class 초기값 설정 방법 - 이 글을 읽기 전 선수 지식 포스팅 DataContext태그 지우기](http://t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png)
3. <Window.Resources> 태그를 생성하고 그 안에 <local:바인딩할클래스명 ~/> 을 만들어 줍니다.
![[c# wpf] xaml에서 class 초기값 설정 방법 - 이 글을 읽기 전 선수 지식 포스팅 클래스 지정](http://t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png)
4. Person 클래스에 존재하는 프러퍼티명과 원하는 value를 각각 할당합니다.
![[c# wpf] xaml에서 class 초기값 설정 방법 - 이 글을 읽기 전 선수 지식 포스팅 초기값 설정](http://t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png)
5. x:Key명을 넣어줍니다.
(해당 부분이 낯선분은 StaticResource 포스팅을 읽고 와주시기 바랍니다.)
![[c# wpf] xaml에서 class 초기값 설정 방법 - 이 글을 읽기 전 선수 지식 포스팅 x:Key명 설정](http://t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png)
6. DataContext를 지정해줍니다.
위에서 <Window.DataContext/>를 지웠기 때문에 다시 DataContext에 연결 해줘야 하는데요.
아래와 같은 방법으로 Resources 태그 안에서 정의한 클래스를 연결 시켜줍니다.
![[c# wpf] xaml에서 class 초기값 설정 방법 - 이 글을 읽기 전 선수 지식 포스팅 datacontext 재연결](http://t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png)
output:
![[c# wpf] xaml에서 class 초기값 설정 방법 - 이 글을 읽기 전 선수 지식 포스팅 [c# wpf] xaml에서 class 초기값 설정 방법 - 이 글을 읽기 전 선수 지식 포스팅](https://blog.kakaocdn.net/dna/LGzcK/btrFchu4ppU/AAAAAAAAAAAAAAAAAAAAALWhj_ye31lsRSpjlcfcUxEvDbB8kHqi6ayR4K1dsxye/img.png?credential=yqXZFxpELC7KVnFOS48ylbz2pIh7yKj8&expires=1753973999&allow_ip=&allow_referer=&signature=GB1taVsI1TdNfMKc2gdZ0DU95i0%3D)
최종값이 정상적으로 출력이 됩니다.
'c# > wpf' 카테고리의 다른 글
[c# wpf] 클래스의 프러퍼티를 콤보박스 아이템으로 넣는 법? (0) | 2022.06.23 |
---|---|
[c# wpf] 콤보박스(combobox) 사용방법 (1) | 2022.06.20 |
[c# wpf] ListView 내용 편집,수정 하는법(with IValueConverter) (0) | 2022.06.10 |
[c# wpf] Control Template 란? / 사용방법 (with Style) (1) | 2022.06.02 |
[c# wpf] slider(슬라이더) 사용법 (with 데이터바인딩) (0) | 2022.05.31 |
댓글