事件NotifyChanged不会使用我的静态变量触发

时间:2013-04-11 19:53:25

标签: c# wpf events data-binding inotifypropertychanged

我创建了一个具有两个静态属性的类:

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));
        }
    }

}

我的问题是,当我使用setThemeGradient()来设置这两个属性时,不会引发notify事件。 (这是为了进行约束)

有人有想法吗?

非常感谢,

致以最诚挚的问候,

Nixeus

2 个答案:

答案 0 :(得分:2)

  

你实际上并没有implement INotifyPropertyChanged ......

你不能这样做static

您应该正确实现INotifyPropertyChanged - 然后绑定到实例的属性。

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

然后使用你的视图模型来获取正确的实例(我不确定CParameters属于哪里,你的vm是怎样的)。

  

或者如果你需要一个实例,你可以通过x:Static - 和Binding进行绑定   向您的班级公开“实例” - 通过“单身人士” - 例如   CParameters.Instance

e.g。

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

虽然我建议您通过视图模型层次结构进行正确的绑定。

注意:
1)典型的INotifyPropertChanged实现
2)添加Singleton' implementation - which is密封的(not required but good),私有ctor(不是必需的,但推荐) - 以及只读静态属性 - 用于外部访问。

答案 1 :(得分:0)

    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));
        }
    }