WPF-22 基于MVVM员工管理-02

我们接着上一节,这节我们实现crud操作,我们在EmployeeViewMode类中新增如下成员,并在构造函数中初始化该成员

f871baa87113095982750de6bc202426.png

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);
}

b2ac446dcf75455da0f722b358a62e6f.png

code snippet

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; }
}

定义每个Command委托执行的方法,code snippet

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;
            }
        }

在EmployeeService 中添加如下方法:

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做绑定

98d51c44c4547d27acea87d0208e5609.png

code snippet

<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>

到这里我们基本完成对一个页面的增删改查

9c8771b137098eaf52f616388cc34152.gif

WPF MVVM 架构中,可以使用 InvokeCommandAction 来触发 ViewModel 中的命令,并且可以传递一个参数。如果需要传递多个参数,可以使用以下方法: 1. 使用命令参数对象 定义一个类,包含需要传递的多个参数,例如: ``` public class CommandParameter { public string Parameter1 { get; set; } public int Parameter2 { get; set; } } ``` 在 XAML 中,使用该类作为 InvokeCommandAction 的 CommandParameter 属性的值: ``` <i:InvokeCommandAction Command="{Binding MyCommand}" CommandParameter="{Binding CommandParameter}" /> ``` 在 ViewModel 中,命令的 Execute 方法可以接收该类的实例作为参数: ``` public RelayCommand<CommandParameter> MyCommand { get; set; } public void MyCommandExecute(CommandParameter parameter) { // 使用参数 } ``` 2. 使用触发事件的参数 在 XAML 中,可以使用 EventTrigger 触发一个事件,并且可以使用 EventTrigger 的 EventArgsConverter 属性将事件参数转换为需要的类型。例如: ``` <i:Interaction.Triggers> <i:EventTrigger EventName="MyEvent"> <i:InvokeCommandAction Command="{Binding MyCommand}"> <i:InvokeCommandAction.CommandParameter> <MultiBinding Converter="{StaticResource MyConverter}"> <Binding Path="Parameter1" /> <Binding Path="Parameter2" /> </MultiBinding> </i:InvokeCommandAction.CommandParameter> </i:InvokeCommandAction> </i:EventTrigger> </i:Interaction.Triggers> ``` 在这里,使用 MultiBinding 将多个绑定值传递给一个转换器。转换器将这些值转换为需要的类型,并且将它们封装到一个对象中,然后作为命令的参数传递给 ViewModel。 在 ViewModel 中,命令的 Execute 方法可以接收该对象作为参数: ``` public RelayCommand<MyParameter> MyCommand { get; set; } public void MyCommandExecute(MyParameter parameter) { // 使用参数 } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值