david hol
david hol

Reputation: 1280

Static INotifyPropertyChanged event not working

I have this Base class:

public abstract class WiresharkFile : BaseObservableObject, IDisposable
{
    private string _fileName;
    private int _packets;
    private int _packetsSent;

    public string FileName
    {
        get { return _fileName; }
        set { _fileName = value; }
    }

    public int Packets
    {
        get { return _packets; }
        set { _packets = value; }
    }

    public int PacketsSent
    {
        get { return _packetsSent; }
        set
        {
            _packetsSent = value;
            OnPropertyChanged();
        }
    }

    public void Dispose()
    {
        // Implemented insde inherit classes.
    }
}

BaseObservableObject:

public class BaseObservableObject : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        var handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }

    protected virtual void OnPropertyChanged<T>(Expression<Func<T>> raiser)
    {
        var propName = ((MemberExpression)raiser.Body).Member.Name;
        OnPropertyChanged(propName);
    }

    protected bool Set<T>(ref T field, T value, [CallerMemberName] string name = null)
    {
        if (!EqualityComparer<T>.Default.Equals(field, value))
        {
            field = value;
            OnPropertyChanged(name);
            return true;
        }
        return false;
    }
}

My collection:

public ObservableCollection<WiresharkFile> wiresharkFiles { get; set; }

So as you can see avery inherit class from my Base class have this PacketsSent property change so in this case all works fine.

Now i have another static property inside WiresharkFile (base class):

private static volatile int _totalPacketsSent;

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

So inside BaseObservableObject i created this member:

public static event PropertyChangedEventHandler StaticPropertyChanged;

And:

protected static void OnStaticPropertyChanged([CallerMemberName] string propertyName = null)
{
    var handler = StaticPropertyChanged;
    if (handler != null) handler(typeof(WiresharkFile), new PropertyChangedEventArgs(propertyName));
}

And inside XAML i want to update my Label:

Content="{Binding Path=(my:WiresharkFile.TotalPacketsSent)}" 

So this is not working so currently this Label is updated via code behind. As i doing something wrong ?

Upvotes: 1

Views: 271

Answers (2)

Clemens
Clemens

Reputation: 128061

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
}

Upvotes: 1

Ilan
Ilan

Reputation: 2772

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,

Upvotes: 0

Related Questions