Static INotifyPropertyChanged event not working

微笑、不失礼 提交于 2019-12-01 13:18:41

The static property changed event has to be declared in class WiresharkFile (i.e. the class that also declares the static property). It won't work if it is declared in a base class.

public class WiresharkFile : BaseObservableObject
{
    public static event PropertyChangedEventHandler StaticPropertyChanged;

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

    // static property here
}

Since your create PropertyChangedEventHandler is no part of a real INotifyPropertyChanged interface, the binding doesn't recognize that the notification event was thrown, thus it doesn't pull any new value from the TotalPacketsSent. To make binding refresh itself try something like this: Put in your WiresharkFile class:

    private static WiresharkFile This;

    public WiresharkFile()
    {
        This = this;
    }
    private static volatile int _totalPacketsSent;

    public static int TotalPacketsSent
    {
        get { return _totalPacketsSent; }
        set
        {
            _totalPacketsSent = value;
            OnStaticPropertyChanged(This);
        }
    }

- Handler code:

protected static void OnStaticPropertyChanged(object sender, [CallerMemberName]string propertyName = null)
    {
        var baseObservable = sender as BaseObservableObject;
        if(baseObservable == null) return;
        baseObservable.OnPropertyChanged(propertyName);
    }

in my opinion you doesn't need the PropertyChangedEventHandler event there in BaseObservableObject class.

regards,

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!