Walter Fabio Simoni
Walter Fabio Simoni

Reputation: 5729

Event NotifyChanged not fire with my static variable

I created a class with two static properties :

public class CParametres
{

    public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged;

    private  static Color m_ThemeColorGradientBegin;
    public  static Color ThemeColorGradientBegin
    {
        get { return m_ThemeColorGradientBegin; }
        set
        {
            m_ThemeColorGradientBegin = value;
            NotifyStaticPropertyChanged("ThemeColorGradientBegin");
        }
    }

    private  static Color m_ThemeColorGradientEnd;
    public  static Color ThemeColorGradientEnd
    {
        get { return m_ThemeColorGradientEnd; }
        set
        {
            m_ThemeColorGradientEnd = value;
            NotifyStaticPropertyChanged("ThemeColorGradientEnd");
        }
    }

    public CParametres()
    {
     ....   
    }

    public void setThemeGradient(Color ColorBegin, Color ColorEnd)
    {
        ThemeColorGradientBegin = ColorBegin;
        ThemeColorGradientEnd = ColorEnd;
    }

    private static void NotifyStaticPropertyChanged(string propertyName)
    {
        if (StaticPropertyChanged != null)
        {
            StaticPropertyChanged(null, new PropertyChangedEventArgs(propertyName));
        }
    }

}

My problem is that when i use the setThemeGradient() in order to set the two properties, the notify event is not raised. ( It's in order to make a binding )

Anyone have an idea please ?

Thanks a lot,

Best regards,

Nixeus

Upvotes: 1

Views: 851

Answers (2)

NSGaga
NSGaga

Reputation: 14302

You didn't actually implement INotifyPropertyChanged...

You cannot do that to be static.

You should properly implement INotifyPropertyChanged - and then bind to properties of an instance.

public sealed class YourClass : INotifyPropertyChanged 
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string propertyName) { this.PropertyChanged.Raise(this, new PropertyChangedEventArgs(propertyName)); } // my 'raise' use typical event handling  

    public static readonly YourClass Instance = new YourClass(); // your ctor params if needed
    private YourClass() // ensure only you can 'construct'
    { }

    // call OnPropertyChanged from your properties
    // implement 'non-static' properties

Use then your view-models to get to the right instance (I'm not sure where CParameters belong to, how your vm looks like).

Or if you need an instance you could do Binding via x:Static - and expose an 'Instance' to your class - via a 'Singleton' - e.g. CParameters.Instance

e.g.

{Binding Path=Property, Source={x:Static my:YourClass.Instance}}  

Though I recommend to do the proper binding via your view-model hierarchy.

Note:
1) So typical INotifyPropertChanged implementation
2) Add the Singleton' implementation - which issealed(not required but good),private` ctor (not required but recommended) - and readonly static property - for outside access.

Upvotes: 2

Darek
Darek

Reputation: 4797

    public event PropertyChangedEventHandler PropertyChanged;

    // This method is called by the Set accessor of each property. 
    // The CallerMemberName attribute that is applied to the optional propertyName 
    // parameter causes the property name of the caller to be substituted as an argument. 
    private void NotifyPropertyChanged(String propertyName)
    {
        if (PropertyChanged != null && !String.IsNullOrEmpty(propertyName))
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

Upvotes: 0

Related Questions