希望有人可以解决问题。在下面的ViewModel中,使用Entity Framework作为我的模型是否消除了使用[Model]和[[ViewModelToModel(...)]属性的需要?代码运行相同或不运行,因为视图中的绑定会忽略它们并绑定到ObservableCollection。
评论
public class MainWindowViewModel : ViewModelBase
{
Models.OneHour_DataEntities ctx;
public MainWindowViewModel()
: base()
{
Save = new Command(OnSaveExecute, OnSaveCanExecute);
ctx = new Models.OneHour_DataEntities();
Customers = new ObservableCollection<Models.Customer>(ctx.Customers);
}
public ObservableCollection<Models.Customer> Customers
{
get { return GetValue<ObservableCollection<Models.Customer>>(CustomersProperty); }
set { SetValue(CustomersProperty, value); }
}
public static readonly PropertyData CustomersProperty = RegisterProperty("Customers", typeof(ObservableCollection<Models.Customer>), null);
public Command Save { get; private set; }
private bool OnSaveCanExecute()
{
return true;
}
private void OnSaveExecute()
{
ctx.SaveChanges();
}
}
答案 0 :(得分:0)
Catel使用不同的界面来利用这些模型。例如,它使用以下接口:
如果您的实体模型实现了这些接口,则可以将属性定义为模型。
但是,在您的示例中,您使用ObservableCollection(因此是模型列表)作为模型。这是不受支持的(或者,集合必须支持IEditableObject和INotifyPropertyChanged)。