llogui
llogui

Reputation: 1

DataBindings betwen two class

Object: Use DataBindings between three class

Environment: Visual Studio 2012, C#, WindowsForms

Error: DataBindings not work in my classes

Expected results:

Objects:

DataBindings:

Area.DataBindings.Add("H",Height,"Q");
Area.DataBindings.Add("B",Base,"Q");
AreaResult.DataBindings.Add("Display",Area,"Calculation");

Height.Q = 5;
Base.Q = 6;

Should produce that Area.Calculation set to 30 and AreaResult.Display too.

Class Quantity code:

public class Quantity:INotifyPropertyChanged
{
   public Nullable<decimal> Q
   {
      get{ return this._q;}
      set
         {
            this._q = value;
            NotifyPropertyChanged("Q");
         }
   }

   public event PropertyChangedEventHandler PropertyChanged;

   protected void NotifyPropertyChanged(string name)
   {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }
}

Class Formulate code:

public class Formulate: INotifyPropertyChanged 
{ 
   public Nullable<decimal> H
    {
        get { return this._h; }
        set
        {
            this._h = value;
            NotifyPropertyChanged("H");
        }
    }
    private Nullable<decimal> _h;


    public Nullable<decimal> Q
    {
        get { return this._q; }
        set
        {
            this._q = value;
            NotifyPropertyChanged("Q");
        }
    }
    private Nullable<decimal> _h;


    public Nullable<decimal> Calculation
    {
        get { return this._calculation; }
        set
        {
            this._calculation = H/Q;
            NotifyPropertyChanged("Caltulation");
        }
    }
    private Nullable<decimal> _calculation;

    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged(string name)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }
}

Class Result code:

public class Result:INotifyPropertyChanged
{
   public Nullable<decimal> Display
   {
      get{ return this._display;}
      set
         {
            this._display = value;
            NotifyPropertyChanged("Display");
         }
   }

   public event PropertyChangedEventHandler PropertyChanged;

   protected void NotifyPropertyChanged(string name)
   {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }
}

Upvotes: 0

Views: 68

Answers (1)

Murven
Murven

Reputation: 2387

This should do the trick:

public class Formulate: INotifyPropertyChanged 
{ 
   public Nullable<decimal> H
    {
        get { return this._h; }
        set
        {
            this._h = value;
            NotifyPropertyChanged("H");
            NotifyPropertyChanged("Calculation");
        }
    }
    private Nullable<decimal> _h;


    public Nullable<decimal> Q
    {
        get { return this._q; }
        set
        {
            this._q = value;
            NotifyPropertyChanged("Q");
            NotifyPropertyChanged("Calculation");
        }
    }
    private Nullable<decimal> _h;


    public Nullable<decimal> Calculation
    {
        get { return _h == null || _q == null ? null : _h.Value / _q.Value; }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged(string name)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }
}

Upvotes: 1

Related Questions