2011-08-16 24 views
1

カスタムwpfコンポーネントのデフォルト値を設定するにはどうすればよいですか? プロパティ "public protection Protection {set; get;}"を持つテキストフィールドがあります。wpfデザイナのカスタムコントロールの既定値を設定する方法?

public class Field3270Attributes{ 
    public enum Protection 
    { 
     PROTECTED, 
     UNPROTECTED, 
     AUTOSKIP 
    } 
} 

AUTOSKIPが、保護すべきであるデフォルト値が列挙型で、その最初の要素以来、WPFデザイナで、デフォルト値として表示されている:保護が列挙型です。 テキストフィールドコンストラクタの保護を設定しても効果がありませんでした。 動作するDependencyPropertyを試しましたが、デフォルト値以外の値を使用する場合はコールバック(setProtection)を指定する必要があります。 コールバックを指定しないと、wpfデザイナ内の値を変更しても効果はありません。 すべてのプロパティに対してコールバックメソッドを指定しなくても同じ動作をする方法はありますか?

public class Textfield{ 

    public static readonly DependencyProperty ProtectionProperty = 
      DependencyProperty.Register("Protection", 
             typeof(Field3270Attributes.Protection), 
             typeof(Textfield3270), 
             new FrameworkPropertyMetadata(Field3270Attributes.Protection.PROTECTED, setProtection)); 

    private static void setProtection(object sender, DependencyPropertyChangedEventArgs e) 
    { 
     Textfield field = (Textfield)sender; 
     field.Protection = (Field3270Attributes.Protection)e.NewValue; 
    } 

    private Field3270Attributes.Protection protection; 

    public Field3270Attributes.Protection Protection 
    { 
     get 
     { 
      return protection; 
     } 

     set 
     { 
      this.protection = value; 

      if (value == Field3270Attributes.Protection.UNPROTECTED) 
      { 
       this.IsReadOnly = false; 
       Background = Brushes.White; 
      } 
      else 
      { 
       this.IsReadOnly = true; 
       Background = Brushes.LightSteelBlue; 
      } 
     } 
    } 

    public Textfield3270() 
    { 

     this.Protection = Field3270Attributes.Protection.PROTECTED; 
    } 

} 

答えて

1

あなたDependencyProperty定義は、デフォルト値を定義します。あなたは、同じ名前を持つ独自のバージョンを実装することで、あなたのProtectionDependencyPropertyを上書きしているFrameworkPropertyMetadata

public static readonly DependencyProperty ProtectionProperty = DependencyProperty.Register("Protection", typeof(Field3270Attributes.Protection), typeof(Textfield3270), new FrameworkPropertyMetadata(Field3270Attributes.Protection.AUTOSKIP, setProtection)); 

AUTOSKIPPROTECTEDからEDIT

の最初のパラメータを変更します。 Protection {get; set;}の定義を完全に削除します。

あなたは彼らがそうのように取得/設定などの静的メソッドを定義し、XAMLデザイナで表示したい場合:

public static Field3270Attributes.Protection GetProtection(DependencyObject obj) 
{ 
    return (Field3270Attributes.Protection)obj.GetValue(ProtectionProperty); 
} 

public static void SetProtection(DependencyObject obj, Field3270Attributes.Protection value) 
{ 
    obj.SetValue(ProtectionProperty, value); 
} 

あなたがにPropertyChangedにいくつかのロジックを添付したい場合は、あなたがこのコードを使用することができますあなたのクラスのコンストラクタ:

private void Protection_Changed() 
{ 
    Field3270Attributes.Protection protection = GetProtection(this); 

    // Do something with value 
} 
+0

ありがとう:

DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(Textfield.ProtectionProperty, typeof(Textfield)); if (dpd != null) dpd.AddValueChanged(this, delegate { Protection_Changed(); }); 

そして、あなたの変更方法は次のようになります。しかし、それは私の記述の単なる誤りであった。私は前にautoskipでそれを持っていましたが、保護されているに変更しましたが、問題は、どのようにデフォルト値を設定して、デフォルト値からwpfデザイナーの他の値への変更も認識できますか?コールバックがなければ、値を変更することはできますが、何も起こりませんが、すべての単一値に対してコールバックを定義したくありません。 – Markus

+0

DependencyPropertyを登録すると、そのプロパティに対してGet/Setが自動的に作成されます。 DependencyPropertyを指し示すデフォルトの 'TextField.Protection'を上書きして、独自のバージョンのプロパティを実装しているようです。あなたのバージョンの 'Protection'プロパティを削除して、動作しているかどうか確認してください。 – Rachel

+0

はい、上のコードでわかるように、保護プロパティのget/setを上書きしています。私はこれを現在自動実装されたプロパティに変更しました: public Field3270Attributes.Protection Protection {set;取得する; } しかし、それはどちらもうまくいきません。フィールドプロパティのみを設定していても、まだコールバックが必要です。 – Markus

関連する問題