两个类之间的DataBindings

时间:2014-12-12 02:28:03

标签: c# winforms data-binding inotifypropertychanged

对象:在三个类

之间使用DataBindings

环境: Visual Studio 2012,C#,WindowsForms

错误: DataBindings无法在我的课程中使用

预期结果:

对象:

  • 高度:数量
  • 基数:数量
  • 区域:制定
  • AreaResult:结果

数据绑定:

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

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

应该将Area.Calculation设置为30和AreaResult.Display

班级数量代码:

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

类制定代码:

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

班级结果代码:

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

1 个答案:

答案 0 :(得分:1)

这应该可以解决问题:

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