Bob.
Bob.

Reputation: 4002

Singleton INotifyPropertyChanged does not change after initial defining and binding

So, I created a Singleton to be used to track a ToolTip enabler that is used globally throughout the entire application, but when I try to bind it to a ToolTip, it binds the value initially, but then when I change the ToolTip enabler value, the binding does not update.

This is the User Control that creates the bind to the ToolTip:

 public partial class ViewTreeClass : UserControl {
      private void addStuff() {
           EnvironmentalVariables.Instance.ToolTipEnable = true;
           Binding bind = new Binding() {
                Source = EnvironmentalVariables.Instance.ToolTipEnable,
           };
           ToolTip x = new ToolTip();
           x.Content = "This is text";
           user.SetBinding(ToolTipService.IsEnabledProperty, bind);
           user.ToolTip = x;
           EnvironmentalVariables.Instance.ToolTipEnable = false;
           // Executing the above statement changes the variable, but ToolTipService.IsEnabledProperty does not change to reflect EnvironmentalVariables.Instance.ToolTipEnable
      }
 }

This is the Singleton that implements INotifyPropertyChanged:

 public class EnvironmentalVariables : INotifyPropertyChanged {
      private static EnvironmentalVariables instance = new EnvironmentalVariables();
      public static EnvironmentalVariables Instance {
           get {
                if(instance == null) {
                     instance = new EnvironmentalVariables();
                }
                return instance;
           }
      }

      private EnvironmentalVariables() { }

      private bool tooltipEnable;
      public bool ToolTipEnable {
           get {
                return tooltipEnable;
           }
           set {
                if(tooltipEnable != value) {
                     tooltipEnable = value;
                     this.RaiseNotifyPropertyChanged("ToolTipEnable");
                }
           }
      }

      private void RaiseNotifyPropertyChanged(string property) {
           if(PropertyChanged != null) {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
           }
      }

      public event PropertyChangedEventHandler PropertyChanged;
 }

When I change 'EnvironmentalVariables.Instance.ToolTipEnable = false;' it changes the Singleton variable to 'false', but it doesn't update ToolTipService.EnabledProperty to 'false' and PropertyChanged is always null. Am I missing a subscription to PropertyChanged somewhere?

The Singleton and the UC are both in different namespaces/class files.

Upvotes: 0

Views: 1873

Answers (2)

Vivien Ruiz
Vivien Ruiz

Reputation: 626

Problem is in the first extract IMO, the way you create the binding. What type is user ?

Set Instance as the Source, and ToolTipEnable as the Path (bind.Path = new PropertyPath("ToolTipEnable")).

And have you a reason for using ToolTipService.IsEnabledProperty instead of ToolTip.IsEnabledProperty ? I'm not sure this is the good place for this trick (but I may be wrong, try both ^^).

Your singleton is wrong (but not causing the problem). I've corrected the way you raise your event too :

 public sealed class EnvironmentalVariables : INotifyPropertyChanged {
      private static readonly EnvironmentalVariables instance = new EnvironmentalVariables();
      public static EnvironmentalVariables Instance {
           get 
           {
               return instance;
           }
      }

      private EnvironmentalVariables() { }

      private bool tooltipEnable;
      public bool ToolTipEnable {
           get {
                return tooltipEnable;
           }
           set {
                if(tooltipEnable != value) {
                     tooltipEnable = value;
                     this.RaiseNotifyPropertyChanged("ToolTipEnable");
                }
           }
      }

      private void RaiseNotifyPropertyChanged(string property) {
           var handler = PropertyChanged;
           if(handler != null) {
                handler (this, new PropertyChangedEventArgs(property));
           }
      }

      public event PropertyChangedEventHandler PropertyChanged;
 }

Upvotes: 3

Jakub
Jakub

Reputation: 534

I believe it has to do with not setting the DataContext of your ViewTreeClass since I don't really see any other errors with the code. Try setting the DataContext to the singleton class at the beginning of your addStuff() method. Also, although tangential to your question, I would really suggest doing what you're doing in the partial class in the control's XAML since creating the binds programatically is usually pretty messy.

Upvotes: 1

Related Questions