【发布时间】:2013-12-14 14:06:13
【问题描述】:
我有一个名为 HaemogramViewModel 的 ViewModel
代码如下:
public class HaemogramViewModel : INotifyPropertyChanged
{
public HaemogramViewModel()
{
}
public Haemogram CurrentHaemogramReport
{
get
{
return MainWindowViewModel.cHaemogram;
}
set
{
MainWindowViewModel.cHaemogram = value;
OnPropertyChanged("CurrentHaemogramReport");
}
}
protected virtual void OnPropertyChanged(string PropertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(PropertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
在我的 MainWindowViewModel 类中:
class MainWindowViewModel : INotifyPropertyChanged
{
public MainWindowViewModel()
{
cHaemogram = new Haemogram();
}
public static Haemogram cHaemogram { get; set; }
private void SaveChanges(object obj)
{
using (Lab_Lite_Entities db = new Lab_Lite_Entities())
{
//db.Patients.Add(CurrentPatient);
if (cHaemogram != null)
{
if (cHaemogram.Haemoglobin != null)
{
db.Haemograms.Add(cHaemogram);
}
}
}
}
}
我的文本框绑定到 CurrentHaemogram 属性的血红蛋白字段。
当我在文本框中输入一些值然后单击保存按钮时,一切正常。
现在的问题是:
当我在文本框中输入一些值时,我按 Tab 键,然后再次单击文本框,然后清除文本框中的值。现在,如果我点击保存按钮,那么我不会得到文本框的值 = null,而是得到文本框的值 = 我之前输入的值。
【问题讨论】: