2017-09-21 3 views
0

WPF에서 사용자 지정 속성을 만들 수있는 방법이 있습니까?WPF 사용자 지정 속성 - 필수 속성

디자이너의 사용자 지정 속성이 채워지지 않았을 때 오류 메시지와 같은 것을 의미합니까? 예 : 필수 = "참/거짓"

내 사용자 정의 속성 정의 :는 무효 (기준이 작업을 수행 더 아웃 - 오브 - 박스 기능이 없습니다

public static readonly DependencyProperty AaFunctionalUnitNameProp; 
    [Category(VsCategoryName.AaObjectInfo)] 
    [Description(VsPropertyDescription.FunctionalUnitName)] 
    public string AaFunctionalUnitName 
    { 
     get => (string)GetValue(AaFunctionalUnitNameProp); 
     set => SetValue(AaFunctionalUnitNameProp, value); 
    } 

답변

1

, 그러나 당신이 할당 할 수 귀하의 정의) 기본값과 귀하의 OnInitialized 이벤트에서 예외가 여전히 기본값이 될 때 던지십시오 (물론 디자인 모드가 아닌 경우에만).

예 :

public class CustomControl : Control 
{ 
    public static readonly DependencyProperty RequiredPropertyProperty = DependencyProperty.Register(
     "RequiredProperty", typeof(int), typeof(CustomControl), new PropertyMetadata(int.MinValue)); 

    public int RequiredProperty 
    { 
     get { return (int) GetValue(RequiredPropertyProperty); } 
     set { SetValue(RequiredPropertyProperty, value); } 
    } 

    protected override void OnInitialized(EventArgs e) 
    { 
     if(RequiredProperty == int.MinValue) 
      if(!DesignerProperties.GetIsInDesignMode(this)) 
       throw new Exception("RequiredProperty must be explicitly set!"); 

     base.OnInitialized(e); 
    } 
} 
+0

확인, 감사합니다! @ManfredRadlwimmer –