【发布时间】:2014-01-01 12:33:24
【问题描述】:
假设我有一个名为 Customer 的类(由实体框架自动生成)
CustomerID // Primary Key & Auto-increment
Name
Gender
City
现在在我的 ViewModel 中:
public Class myViewModel : INotifyPropertyChanged
{
public Customer CurrentCustomer
{
get
{
return MainViewModel.cCustomer;
}
set
{
myViewModel.cCustomer = value;
OnPropertyChanged("CurrentCustomer");
}
}
....
....
....
}
这是我的 MainViewModel
public Class MainViewModel : INotifyPropertyChanged
{
public MainViewModel()
{
cCustomer = new Customer();
}
public static Customer cCustomer { get; set; }
public void SaveChanges()
{
//Here I want to check if all the fields of cCustomer are empty
//And depending on that I want to save the cCustomer.
}
....
....
....
}
我试图通过将 cCustomer 的所有字段与 null 进行比较来检查它们,但我收到一个错误,指出对象引用未设置为对象的实例。
简而言之,我想在保存客户时检查 cCustomer 是否为空。
【问题讨论】:
-
if (cCustomer != null)不起作用? -
附加调试器,看看什么是null?
-
cCustomer 是否已实例化或初始化?
-
@Tim (!(cCustomer == null)) 阅读起来太麻烦了。也许你想要 (cCustomer != null)
-
如果
cCustomer为null,您将无法检查字段,因此您需要先查看包含对象(本例中为cCustomer)是否为null。如果不是,那么您可以检查各个属性。
标签: c# wpf silverlight mvvm