ハロの外部記憶インターフェイス

そろそろ覚える努力が必要かも…

カスタムコントロールにDependencyProperty追加

カスタムコントロールにDependencyPropertyを追加する

DependencyPropertyはstaticで宣言されるプロパティで、コントロール自身が初期化されて居ない状態で共通で使われるプロパティである。

public static readonly DependencyProperty {プロパティ名}Property

DependencyProperrtyを宣言する。基本プロパティ名が付く、初期値と変更時のイベント処理を指定する

public static readonly DependencyProperty {プロパティ名}Property = 
  DependencyProperty.Register("{プロパティ名}", 
                              typeof({プロパティ名の型}), 
                              typeof({カスタムコントロールクラス名}),
                              new FrameworkPropertyMetadata({初期値}, new PropertyChangedCallback({プロパティ名}PropertyChanged))
                              )

ex)
public static readonly DependencyProperty IsSelectedProperty = 
  DependencyProperty.Register("IsSelected", 
                              typeof(bool), 
                              typeof(myControl),
                              new FrameworkPropertyMetadata(false, new PropertyChangedCallback(IsSelectedPropertyChanged))
                              )

private static void {プロパティ名}PropertyChanged()

DepedencyPropertyのCallBackに指定する。イベント処理

private static IsSelectedPropertyChanged(ByVal sender As DependencyObject, ByVal args As DependencyPropertyChangedEventArgs)
{
    myControl o = (myControl)sender
    
    o.IsSelected = args.NewValue
    
}
  1. 追記:画面表示時(初期化以降)、このプロパティ変更処理(IsSelectedPropertyChanged()が実行されると、o.IsSelected = args.NewValueにより、IsSelectedのBinding情報が切れる事が解った。自分自身への置換は注意が必要

public bool {プロパティ名}

プロパティの値を保存するプロパティ宣言

public bool IsSelected
{
    set {SetValue(IsSelectedProperty, value); }
    get { return GetValue(IsSelectedProperty); }
}
  1. 追記:プロパティの処理は画面配置時のプロパティ値の変更時の処理として、適用される。(デザイン画面上で表示変更を適用したい場合、ここに修正が必要ってこと