MVVM使用ICommand保存在数据库中
问题描述:
我有一个对话框来获取一些基本信息,用于在点击保存按钮时将这些信息保存在数据库中。 这些基本信息将通过绑定存储在ViewModel中。我通过Entity Framework处理数据库,并通过DataContext处理数据库。MVVM使用ICommand保存在数据库中
private Command.MonitoringTaskCommand objSaveButtonCommand =
new Command.MonitoringTaskCommand(
new Action<DataModel.MonitoringTask>(x => {
DataModel.MonitorContext context = new DataModel.MonitorContext();
context.MonitoringTasks.Add(x);
context.SaveChanges();
}),
new Func<bool>(() => {
Debug.WriteLine("Todo: Validate data... ");
return true;
}));
我坚持保存执行。
- 我如何可以传递数据的命令?
我知道有对XAML水平CommandParameter,但数据存储在视图模型,并可以从一个在XAML水平不同。从你的命令的结合
答
数据传递,从而在您的绑定是使用CommandParameter到对象绑定
检查这个教程
http://www.c-sharpcorner.com/UploadFile/e06010/wpf-icommand-in-mvvm/
XAML视图元素应该是数据绑定您的视图模型的属性(和如果需要更新这些)。还要考虑使用数据访问层来访问您的EF上下文。命令参数是要走的路。 http://*.com/questions/12371253/how-do-i-pass-a-variable-as-a-commandparameter – Milen