Step 1: Creating the Model

Create a new class named Sale, which implements the INotifyPropertyChanged interface.

 

public class Sale : INotifyPropertyChanged

{

    private string _product;

    private double _value;

    private double _discount;

 

    public Sale(string product, double value, double discount)

    {

        Product = product;

        Value = value;

        Discount = discount;

    }

 

    public string Product

    {

        get { return _product; }

        set

        {

            if (_product != value)

            {

                _product = value;

                OnPropertyChanged("Product");

            }

        }

    }

 

    public double Value

    {

        get { return _value; }

        set

        {

            if (_value != value)

            {

                _value = value;

                OnPropertyChanged("Value");

            }

        }

    }

 

    public double Discount

    {

        get { return _discount; }

        set

        {

            if (_discount != value)

            {

                _discount = value;

                OnPropertyChanged("Discount");

            }

        }

    }

 

    public event PropertyChangedEventHandler PropertyChanged;

 

    void OnPropertyChanged(string propertyName)

    {

        if (PropertyChanged != null)

            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

    }

}

This class has several properties which define a Sale, including Product, Value and Discount.

By implementing INotifyPropertyChanged this enables binding properties to automatically reflect dynamic changes. For each property you want change notifications for, you call OnPropertyChanged whenever the property is updated. Note that ObservableCollections already inherit INotifyPropertyChanged.


Send us comments about this topic.
Copyright © GrapeCity, inc. All rights reserved.