Monday, February 17, 2014

INotifyPropertyChanged implementation for view model value objects

Please see the ObservableCollection example for automatically displaying trades updates in a data grid with MVVM here:

http://wpfgrid.blogspot.com/2014/02/simple-observablecollection-wpf-mvvm.html


Continuing that exmaple, we can now implement INotifyPropertyChanged interface for "Trade" class as below.





    public class Trade : ITrade, INotifyPropertyChanged
    {
        private string _side;
        private string _ticker;
        private string _qty;
        private string _price;
        private string _mktprice;
        private string _trader;
        private string _sales;       
        private string _tradeType;
        private string _status;

        public string Side
        {
            get { return _side; }
            set
            {
                if (_side != value)
                {
                    _side = value;
                    OnPropertyChanged("Side");
                }
            }
        }
       
        public string Ticker
        {
            get { return _ticker; }
            set
            {
                if (_ticker != value)
                {
                    _ticker = value;
                    OnPropertyChanged("Ticker");
                }
            }
        }

        public string Qty
        {
            get { return _qty; }
            set
            {
                if (_qty != value)
                {
                    _qty = value;
                    OnPropertyChanged("Qty");
                }
            }
        }

        public string Price
        {
            get { return _price; }
            set
            {
                if (_price != value)
                {
                    OnPropertyChanged("Price");
                }
            }
        }

        public string MktPrice
        {
            get { return _mktprice; }
            set
            {
                if (_mktprice != value)
                {
                    _mktprice = value;
                    OnPropertyChanged("MktPrice");
                }
            }
        }

        public string Trader
        {
            get { return _trader; }
            set
            {
                if (_trader != value)
                {
                    _trader = value;
                    OnPropertyChanged("Trader");
                }
            }
        }
       
        public string Sales
        {
            get { return _sales; }
            set
            {
                if (_sales != value)
                {
                    _sales = value;
                    OnPropertyChanged("Sales");
                }
            }
        }


        public string TradeType
        {
            get { return _tradeType; }
            set
            {
                if (_tradeType != value)
                {
                    _tradeType = value;
                    OnPropertyChanged("TradeType");
                }
            }
        }


        public string Status
        {
            get { return _status; }
            set
            {
                if (_status != value)
                {
                    _status = value;
                    OnPropertyChanged("Status");
                }
            }
        }


        public event PropertyChangedEventHandler PropertyChanged;       
        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }





However just to make life simpler I have a BaseViewModel class to take care of  INotifyPropertyChanged implementation and also provides convenient methods to set and raise property change notification. Instead of coding INotifyPropertyChanged  in each class, we can just use this base class.



    public class BaseViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        protected void SetAndNotify<T>(ref T field, T value, string propertyName)
        {
            if (!EqualityComparer<T>.Default.Equals(field, value))
            {
                field = value;
                OnPropertyChanged(propertyName);
            }
        }
    }



After using this base class, our above (on top) Trade class code looks like below:



    public class Trade : BaseViewModel, ITrade
    {
        private string _side;
        private string _ticker;
        private string _qty;
        private string _price;
        private string _mktprice;
        private string _trader;
        private string _sales;       
        private string _tradeType;
        private string _status;

        public string Side
        {
            get { return _side; }
            set { this.SetAndNotify(ref _side, value, "Side"); }
        }
       
        public string Ticker
        {
            get { return _ticker; }
            set { this.SetAndNotify(ref _ticker, value, "Ticker"); }
        }

        public string Qty
        {
            get { return _qty; }
            set { this.SetAndNotify(ref _qty, value, "Qty"); }
        }

        public string Price
        {
            get { return _price; }
            set { this.SetAndNotify(ref _price, value, "Price"); }
        }

        public string MktPrice
        {
            get { return _mktprice; }
            set { this.SetAndNotify(ref _mktprice, value, "MktPrice"); }
        }

        public string Trader
        {
            get { return _trader; }
            set { this.SetAndNotify(ref _trader, value, "Trader"); }
        }
       
        public string Sales
        {
            get { return _sales; }
            set { this.SetAndNotify(ref _sales, value, "Sales"); }
        }

        public string TradeType
        {
            get { return _tradeType; }
            set { this.SetAndNotify(ref _tradeType, value, "TradeType"); }
        }

        public string Status
        {
            get { return _status; }
            set { this.SetAndNotify(ref _status, value, "Status"); }
        }
    }

1 comment:

  1. LuckyClub: Casino site - Live dealer casino
    Lucky Club. Live Dealer Casino Software. Live Casino Software. LuckyClub.com. Live Casino Software. luckyclub Casumo. Online Casino Software. Online Casino Software. LuckyClub.com.

    ReplyDelete