2016-05-29 12 views
0

モデルのインスタンスにバインドしようとしているブール値プロパティを持つカスタムボタンがあります。すべてが正しいと思われますが、それはプロパティの変更をキャッチしていません...ソース値が変更されたときにDataBound依存関係プロパティが更新されない

MyControl.BooleanPropertySource.BooleanPropertyが変更されたときにMyControl.BooleanPropertyと一致するように更新されます。

<Window 
    ... 
    xmlns:p="clr-namespace:FooProject.Properties" 
    DataContext="{x:Static p:Settings.Default}"> 
    <MyControls:GlassButton   
     Pulsing="{Binding Pulse}"/> 
</Window> 

アプリケーション設定内には、「パルス」(ブール値プロパティ)というプロパティがあります。

これは私の制御に関連するソースコードです:

public class GlassButton : Button { 
    #region Dependency Properties   
    public static readonly DependencyProperty 
     //A whooole lot of irrelevant stuff... 
     PulsingProperty = DependencyProperty.Register(
      "Pulsing", typeof(bool), typeof(GlassButton), 
      new FrameworkPropertyMetadata(false)), 
     //Lots more irrelevant stuff 

    [Category("Pulse")] 
    public bool Pulsing{ 
     get{ return (bool)(this.GetValue(PulsingProperty)); 
     set{ 
      if (value) 
       this.BeginAnimation(BackgroundProperty, this._baPulse); 
      else 
       this.BeginAnimation(BackgroundProperty, null);  
      this.SetValue(PulsingProperty, value); 
     } 
    } 
    //And a pile of more irrelevant stuff. 

私はPulsingセッターに設定されたブレークポイントを持っていますが、彼らはヒットしない飽きない...

それはベア内かどうかを、一貫して動作していますこのような骨のアプリケーション、または本物の正直な本物のアプリケーションに...

なぜバインディングは取れていないのですか?

答えて

1

プロパティの更新を取得するためにインスタンスセッターに頼るべきではありません。データバインディングはこれらのインスタンスプロパティセッターの周りで動作するためです。データバインディングからプロパティの更新を取得するには、プロパティを登録するときにPropertyChangedCallbackPropertyMetadataを指定する必要があります。このように:

public static readonly DependencyProperty PulsingProperty = 
     DependencyProperty.Register("Pulsing", typeof (bool), typeof (GlassButton), new PropertyMetadata(false, OnPulsingPropertyChanged)); 

    //keep it clean 
    public bool Pulsing 
    { 
     get 
     { 
      return (bool) GetValue(PulsingProperty); 
     } 
     set 
     { 
      SetValue(PulsingProperty, value); 
     } 
    } 

    //here you get your updates 
    private static void OnPulsingPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     var glassButton = (GlassButton)d; 
     var newPulsingValue = (bool)e.NewValue; 
     if (newPulsingValue) 
      glassButton.BeginAnimation(BackgroundProperty, glassButton._baPulse); 
     else 
      glassButton.BeginAnimation(BackgroundProperty, null); 
    } 
+0

私はそれを見なければならないでしょうが、意味があります...ありがとう。 – Will

+0

それが問題でした。ありがとう(私は自分自身が嫌い)。 – Will

関連する問題