카테고리 없음

FrameworkPropertyMetadata

유정쟝 2023. 8. 17. 02:36

DependencyProperty를 선언할 때 FrameworkPropertyMetadata의 생성자에 기본값을 설정하면 해당 속성의 형식에 따라 메타데이터에서 기본값이 지정됩니다. 하지만 string 형식에 대해서는 이러한 메커니즘이 적용되지 않습니다.

string 형식은 참조 형식이기 때문에 null이 기본값이라서 메타데이터에서 기본값을 설정할 수 없습니다.

 

따라서 string 형식의 DependencyProperty를 정의할 때는 기본값을 성정하지 않은 것이 일반적입니다.이렇게 하면 기본적으로 null로 초기화되어 값을 바인딩하거나 직접 설정할 수 있습니다.

 

 static CustomControl1()
 {
     IdProperty = DependencyProperty.Register("Id", typeof(int), typeof(CustomControl1), new FrameworkPropertyMetadata(12345)); //


     AgeProperty = DependencyProperty.Register("Age", typeof(int), typeof(CustomControl1), new FrameworkPropertyMetadata(54321));  


     GenderProperty = DependencyProperty.Register("Gender", typeof(bool), typeof(CustomControl1), new FrameworkPropertyMetadata(true));

     AddressProperty = DependencyProperty.Register("Address", typeof(string), typeof(CustomControl1), new FrameworkPropertyMetadata("busan"));

     IrumProperty = DependencyProperty.Register("Irum", typeof(string), typeof(CustomControl1), new FrameworkPropertyMetadata("유정"));

     DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl1), new FrameworkPropertyMetadata(typeof(CustomControl1)));
 }