david hol
david hol

Reputation: 1280

Where i need to define INotifyPropertyChanged in case of Base and sub classes

i have this Base class:

public abstract class WiresharkFile
{
    protected string _fileName;
    protected int _packets;
    protected int _packetsSent;
    protected string _duration;

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

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

And this sub class:

public class Libpcap : WiresharkFile, IDisposable, IEnumerable<WiresharkFilePacket>
{
    ....
}

Create my object:

WiresharkFile wiresahrkFile = new Libpcap(file);

My collection:

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

Send packets:

wiresahrkFile.Sendpackets();

At this point all my wiresahrkFile (Libpcap type) properties is changing so i wonder where i need to define this INotifyPropertyChanged.

Upvotes: 1

Views: 1434

Answers (2)

fatihyildizhan
fatihyildizhan

Reputation: 8832

Same answer with IIan but for C# 8 and .Net Framework 4.8.

1. Base Model

public class ObservableObject : INotifyPropertyChanged
{
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        protected virtual void OnPropertyChanged<T>(Expression<Func<T>> raiser)
        {
            string 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;
        }
}

2. Your Model

public class Current : ObservableObject
{
        private string _status;

        public Current()
        {
            Status = "Not Connected";
        }

        public string Status
        {
            get { return _status; }
            set
            {
                _status = value;
                OnPropertyChanged(); // call this to update
            }
        }
}

3. How to use?

<Label Content="{Binding Status}"/>

Upvotes: 0

Ilan
Ilan

Reputation: 2782

If your xaml is binded to properties of WiresharkFile then a WiresharkFile have to implement the INotifyPropertyChanged, if not it will lead to the memory leaks (Top 3 Memory Leak Inducing Pitfalls of WPF Programming). If your binding is defined only on a Libpcap class then the Libpcap have to implement the INotifyPropertyChanged interface. In my projects I create a base implementation of the INotifyPropertyChanged interface ,and then each base models and base view models just inherits from that implementation. Here some base code: 1. Base implementation:

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

2. Your model (in my opinion):

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

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

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

regards,

Upvotes: 3

Related Questions