/// <summary> /// 处理向此雇员添加新联系人详细信息的操作 /// </summary> /// <typeparam name="T">要添加的联系人详细信息的类型</typeparam> private void AddContactDetail <T>() where T : ContactDetail { ContactDetail detail = this.unitOfWork.CreateObject <T>(); this.unitOfWork.AddContactDetail(this.Model, detail); ContactDetailViewModel vm = ContactDetailViewModel.BuildViewModel(detail); this.ContactDetails.Add(vm); this.CurrentContactDetail = vm; }
/// <summary> /// 初始化 EmployeeViewModel 类的新实例。 /// </summary> /// <param name="employee">此 ViewModel 基于的基础雇员</param> /// <param name="managerLookup">要用于查找经理的现有雇员集合</param> /// <param name="departmentLookup">要用于查找部门的现有部门集合</param> /// <param name="unitOfWork">用于管理更改的 UnitOfWork</param> public EmployeeViewModel(Employee employee, ObservableCollection <EmployeeViewModel> managerLookup, ObservableCollection <DepartmentViewModel> departmentLookup, IUnitOfWork unitOfWork) : base(employee) { if (employee == null) { throw new ArgumentNullException("employee"); } this.unitOfWork = unitOfWork; this.ManagerLookup = managerLookup; this.DepartmentLookup = departmentLookup; // 生成联系人详细信息的数据结构 this.ContactDetails = new ObservableCollection <ContactDetailViewModel>(); foreach (var detail in employee.ContactDetails) { ContactDetailViewModel vm = ContactDetailViewModel.BuildViewModel(detail); if (vm != null) { this.ContactDetails.Add(vm); } } // 响应从此 ViewModel 外部进行的任何更改 this.DepartmentLookup.CollectionChanged += (sender, e) => { if (e.OldItems != null && e.OldItems.Contains(this.Department)) { this.Department = null; } }; this.ManagerLookup.CollectionChanged += (sender, e) => { if (e.OldItems != null && e.OldItems.Contains(this.Manager)) { this.Manager = null; } }; this.AddEmailAddressCommand = new DelegateCommand((o) => this.AddContactDetail <Email>()); this.AddPhoneNumberCommand = new DelegateCommand((o) => this.AddContactDetail <Phone>()); this.AddAddressCommand = new DelegateCommand((o) => this.AddContactDetail <Address>()); this.DeleteContactDetailCommand = new DelegateCommand((o) => this.DeleteCurrentContactDetail(), (o) => this.CurrentContactDetail != null); }