2017-09-21 10 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

OK、ありがとうございました! @ManfredRadlwimmer –

関連する問題