code snippet.
public EmployeeViewMode()
{
employeeService = new EmployeeService();
BindData();
Employee = new Employee();
AddCommand = new DelegateCommand(Add);
GetEmployeeCommand = new DelegateCommand(GetEmployee);
UpdateCommand = new DelegateCommand(Update);
DeleteCommand = new DelegateCommand(Delete);
}
private ICommand addCommand;
public ICommand AddCommand
{
get { return addCommand; }
set { addCommand = value; }
}
private ICommand getEmployeeCommand;
public ICommand GetEmployeeCommand
{
get { return getEmployeeCommand; }
set { getEmployeeCommand = value; }
}
private ICommand updateCommand;
public ICommand UpdateCommand
{
get { return updateCommand; }
set { updateCommand = value; }
}
private ICommand deleteCommand;
public ICommand DeleteCommand
{
get { return deleteCommand; }
set { deleteCommand = value; }
}
public void Add()
{
try
{
var emp = new Employee()
{
No = Employee.No,
Name = Employee.Name,
Role = Employee.Role,
};
bool isAdded = employeeService.Add(emp);
if (isAdded)
{
BindData();
Message = "添加成功";
}
else
Message = "添加失败";
}
catch (Exception ex)
{
Message = ex.Message;
}
}
public void GetEmployee()
{
try
{
var employee = employeeService.GetEmployee(Employee.No);
if (employee != null)
{
var emp = new Employee()
{
No = employee.No,
Name = employee.Name,
Role = employee.Role,
};
this.Employee = emp;
}
else
Message = "未发现当前用户";
}
catch (Exception ex)
{
Message = ex.Message;
}
}
public void Update()
{
try
{
var emp = new Employee()
{
No = Employee.No,
Name = Employee.Name,
Role = Employee.Role
};
bool isUpdated = employeeService.Update(emp);
if (isUpdated)
{
BindData();
Message = "修改成功";
}
else
Message = "修改失败";
}
catch (Exception ex)
{
Message = ex.Message;
}
}
private void Delete()
{
try
{
bool isDeleted = employeeService.Delete(Employee.No);
Employee.No = String.Empty;
Employee.Name = String.Empty;
Employee.Role = String.Empty;
if (isDeleted)
{
BindData();
Message = "修改成功";
}
else
Message = "修改失败";
}
catch (Exception ex)
{
Message = ex.Message;
}
}
public bool Add(Employee employee)
{
_employees.Add(employee);
return true;
}
public bool Update(Employee employee)
{
bool isUpdated = false;
foreach (var item in _employees)
{
if (item.No == employee.No)
{
item.Name = employee.Name;
item.Role = employee.Role;
isUpdated = true;
break;
}
}
return isUpdated;
}
public bool Delete(string no)
{
bool isDeleted = false;
for (int i = 0; i < _employees.Count; i++)
{
if (_employees[i].No == no)
{
_employees.RemoveAt(i);
isDeleted = true;
}
}
return isDeleted;
}
public Employee? GetEmployee(string no)
=> _employees.FirstOrDefault(x => x.No == no);
接下来我们将ViewModel中的Command和EmployeeView.xaml UI做绑定
<Button Content="新增" Width="100" Height="30" Margin="2" Command="{Binding Path=AddCommand}"></Button>
<Button Content="修改" Width="100" Height="30" Margin="2" Command="{Binding Path=UpdateCommand}"></Button>
<Button Content="删除" Width="100" Height="30" Margin="2" Command="{Binding Path=DeleteCommand}"></Button>
<Button Content="查询" Width="100" Height="30" Margin="2" Command="{Binding Path=GetEmployeeCommand}"></Button>
到这里我们基本完成对一个页面的增删改查