C# 如何实现Icommand以添加新客户?
我是MVVM模式的新手,我正在尝试实现一个代码,将新客户添加到数据库中。 我的XAML文件中有两个按钮和两个文本框,一个按钮用于更新,另一个按钮用于使用文本框的文本添加新客户。但是当我点击“添加”按钮时,什么也没有发生,客户也没有添加到我的数据库中。我使用的是EntityFramework和CodeFirst。 这是我的密码: 型号:C# 如何实现Icommand以添加新客户?,c#,wpf,entity-framework,mvvm,ef-code-first,C#,Wpf,Entity Framework,Mvvm,Ef Code First,我是MVVM模式的新手,我正在尝试实现一个代码,将新客户添加到数据库中。 我的XAML文件中有两个按钮和两个文本框,一个按钮用于更新,另一个按钮用于使用文本框的文本添加新客户。但是当我点击“添加”按钮时,什么也没有发生,客户也没有添加到我的数据库中。我使用的是EntityFramework和CodeFirst。 这是我的密码: 型号: public class Customer { [Key] public int CustomerId { get; set; } pu
public class Customer
{
[Key]
public int CustomerId { get; set; }
public string FullName { get; set; }
public string Phone { get; set; }
}
视图模型:
public class CustomerViewModel : ViewModelBase
{
private List<Customer> _customers;
private Customer _currentCustomer;
private CustomerRepository _repository;
public CustomerViewModel()
{
_repository = new CustomerRepository();
_customers = _repository.GeCustomers();
WireCommands();
}
private void WireCommands()
{
UpdateCustomerCommand = new RelayCommand(UpdateCustomer);
}
public RelayCommand UpdateCustomerCommand { get; private set; }
public RelayCommand AddCustomerCommand { get; private set; }
public List<Customer> Customers
{
get { return _customers; }
set { _customers = value; }
}
public Customer CurrentCustomer
{
get { return _currentCustomer; }
set
{
if (_currentCustomer != value)
{
_currentCustomer = value;
OnPropertyChanged("CurrentCustomer");
UpdateCustomerCommand.IsEnabled = true;
}
}
}
public void UpdateCustomer()
{
_repository.UpdateCustomer(CurrentCustomer);
}
public void AddCustomer(Customer customer)
{
_repository.NewCustomer(customer);
}
public类CustomerViewModel:ViewModelBase
{
私人客户名单;
私人客户——当前客户;
私人客户存储库;
公共CustomerServiceModel()
{
_repository=new CustomerRepository();
_customers=_repository.GeCustomers();
wire命令();
}
私有命令()
{
UpdateCustomerCommand=新的RelayCommand(UpdateCustomer);
}
public RelayCommand UpdateCustomerCommand{get;private set;}
public RelayCommand addcustomer命令{get;private set;}
公开名单客户
{
获取{return\u customers;}
设置{u customers=value;}
}
公共客户当前客户
{
获取{return\u currentCustomer;}
设置
{
如果(_currentCustomer!=值)
{
_当前客户=价值;
OnPropertyChanged(“当前客户”);
UpdateCustomerCommand.IsEnabled=true;
}
}
}
public void UpdateCustomer()
{
_repository.UpdateCustomer(当前客户);
}
公共无效添加客户(客户)
{
_新客户(customer);
}
存储库:
public List<Customer> GeCustomers()
{
return _customers;
}
public void UpdateCustomer(Customer SelectedCustomer)
{
Customer customerToChange = _customers.Single(c => c.CustomerId == SelectedCustomer.CustomerId);
customerToChange = SelectedCustomer;
}
public void NewCustomer(Customer customer)
{
_context.Customers.Add(customer);
_context.SaveChanges();
}
公共客户列表()
{
退回客户;
}
public void UpdateCustomer(客户选择的客户)
{
Customer customerToChange=\u customers.Single(c=>c.CustomerId==SelectedCustomer.CustomerId);
customerToChange=所选客户;
}
公共无效新客户(客户)
{
_context.Customers.Add(客户);
_SaveChanges();
}
但是,当我单击按钮时,什么也没有发生,客户也没有添加到数据库中。您的AddCustomer命令没有连接……无论如何,当您仍然有问题时,请使用调试器并逐步完成整个过程。同时,请查看输出窗口以防绑定错误。这很有效,谢谢,但我还有其他问题,代码如何知道ws-文本框中的文本是否等于my类的my Property?您需要在Customer类(和/或专用CustomServiceWModel)上实现INotifyPropertyChanged。