/// <summary> /// 添加 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BtnAdd_OnClick(object sender, RoutedEventArgs e) { var setting = new MetroDialogSettings { AnimateShow = true, AnimateHide = true, AffirmativeButtonText = "确定", NegativeButtonText = "取消", DefaultButtonFocus = MessageDialogResult.Negative }; var selectedItem = (DeptAllResponseModel)TvDept.SelectedItem; if (!_clsLoginModel.PermissionsFunc(_menuId, (int)PermissionsEnum.添加)) { _metroWindow.ShowMessageAsync("友情提示", "您没有添加部门信息的权限"); } else if (selectedItem == null) { _metroWindow.ShowMessageAsync("友情提示", "请选择相应的节点作为父级节点"); } else { _metroWindow.ShowInputAsync("部门新增", "部门名称", setting).ContinueWith(t => { Dispatcher.Invoke((Action)(() => { var deptName = t.Result; if (deptName != null) { var result = _deptManage.Add(new DeptAddRequestModel { DeptName = deptName, ParentId = selectedItem.DeptId }); if (result.ResultStatus == ResultStatus.Success) { var item = new DeptAllResponseModel { DeptId = result.Data.Id, DeptName = result.Data.Name, ParentId = result.Data.PId, Nodes = new ObservableCollection <DeptAllResponseModel>() }; if (selectedItem.DeptId == 0) { _tvDeptItemsSource.Add(item); } else { selectedItem.Nodes.Add(item); } } _metroWindow.ShowMessageAsync(result.ResultStatus == ResultStatus.Success ? "成功提示" : "失败提示", result.Message); } })); }); } }
private void AddNodes(List <Dept> list, int parentId, ObservableCollection <DeptAllResponseModel> nodes) { list.Where(a => a.PId == parentId).OrderBy(a => a.Id).ToList().ForEach(a => { var node = new DeptAllResponseModel { DeptId = a.Id, DeptName = a.Name, ParentId = a.PId, Nodes = new ObservableCollection <DeptAllResponseModel>() }; nodes.Add(node); AddNodes(list, a.Id, node.Nodes); }); }
/// <summary> /// 删除选中的节点 /// </summary> /// <param name="collection"></param> /// <param name="model"></param> private void RemoveDeptAllResponseModel(ObservableCollection <DeptAllResponseModel> collection, DeptAllResponseModel model) { if (collection.Count(a => a.DeptId == model.DeptId) > 0) { _tvDeptItemsSource.Remove(model); } else { RemoveDeptAllResponseModel(collection, model); } }