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.
Problem is in the first extract IMO, the way you create the binding. What type is
user?Set
Instanceas the Source, andToolTipEnableas the Path (bind.Path = new PropertyPath("ToolTipEnable")).And have you a reason for using
ToolTipService.IsEnabledPropertyinstead ofToolTip.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 :