C#/WPF

[WPF] Create Custom Controls

딸기우유중독 2025. 2. 11. 14:49

 

방법1.

ItemsControl에 Resoure연결

 

Custom Control

 

EX) 예제

 

HomeView 

 

HomeView.xaml
0.00MB
HomeView.xaml.cs
0.00MB

 

 

 

StatusItemTemplate.xaml
0.00MB

 

 


 

방법2.

 

Custom Control 만들기

 


 

 

https://www.youtube.com/watch?v=t8zB_SYGOF0

 

 

 


 

 

 

UserControl

 

 

UserControl 에서 

ItemsSource 컨트롤의 DataContext는 

UserControl의 DataContext를 바라볼건데

UserControl의 DP 인 ItemsSource 는 UserControl의 DataContext에 있는 애가 아님.

UserControl Self에 있는애라서 ItemsSource 컨트롤의 DataContext를

UserControl Self에 연결해 줘야

View에서 UserControl ItemsSource에 바인딩하면 ItemsControl의 ItemsSource에 바인딩 됨.

 

 

UserControl 이랑 ItemsControl 이 같은 DataContext 바라 봄.

 

ItemsControl의 DataContext가  UserControl Self를 바라 봄.

 

 

 

아래 줘야 바인딩 제대로 들어감

 RelativeSource={RelativeSource AncestorType=UserControl}

 

 

or

 


 

타입에 따른 PropertyMetadata 를 맞춰줘야 함.

 

EX)

타입에 대한 기본 값 정의가 맞아야 함.

(값 )int 면 0

(참조) object 면 null

 

 

 

 

EX) string 

 

 

 

 


 

 

https://stackoverflow.com/questions/9460034/custom-itemssource-property-for-a-usercontrol

 

Custom ItemsSource property for a UserControl

Does anyone know how to make a custom ItemsSource? What I want to do is to make an itemsSource to my own UserControl so that it could be bound by ObservableCollection<>. Also, I could know

stackoverflow.com

 

 

https://forum.dotnetdev.kr/t/wpf-user-control-dependency-property/7650

 

WPF User Control Dependency Property 오류

안녕하십니까. 제 Applcation에는 8개의 같은 모양의 Toggle button이 존재합니다. User Control로 만들어서 사용중이고, Command와 Command Parameter를 Dependency Property로 만들어서 사용하려고 합니다. 8개의 Toggle

forum.dotnetdev.kr

 

 

https://stackoverflow.com/questions/53642424/bind-itemscontrol-itemsource-to-usercontrol-dependency-property

 

Bind ItemsControl ItemSource to UserControl Dependency Property

this is my very first shot at trying to create a User Control with Dependency Properties. So excuse my lack of knowledge over the subject. I created a general design on one of my pages that I would...

stackoverflow.com

 

 

728x90