【问题标题】:DataBindings betwen two class两个类之间的数据绑定
【发布时间】:2015-02-10 16:58:13
【问题描述】:

对象:在三个类之间使用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));
        }
    }
}

【问题讨论】:

  • 你忘了问问题
  • 也许不是问题的根源,但您是否注意到 NotifyPropertyChanged("Caltulation");这将在稍后返回给你。

标签: c# winforms data-binding inotifypropertychanged


【解决方案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));
        }
    }
}

【讨论】:

  • 感谢您的回答,显示错误类别:没有定义数据绑定
  • 我尝试使用你的建议@Murven,但我得到同样的错误“没有数据绑定的定义”
猜你喜欢
  • 1970-01-01
  • 2014-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-20
  • 2017-08-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多