【发布时间】: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