/// <summary> /// 递归查询设备所属的设备类型编码 /// </summary> /// <param name="smodel"></param> /// <returns></returns> private string GetEquTypeCode(SomeHierarchyViewModel smodel) { if (smodel != null) { if (smodel.Parent != null) { SomeHierarchyViewModel shv = smodel.Parent; ComBoxTreeModel cbtModel = (shv.obj as TreeNode).Target as ComBoxTreeModel; if (cbtModel.Type.Equals(IoConfigParam.Type_EquType)) { return(cbtModel.Code); } else { return(GetEquTypeCode(shv)); } } else { return(string.Empty); } } else { return(string.Empty); } }
/// <summary> /// 下拉树选择改变事件 /// </summary> private void comboBoxTree_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (e.AddedItems != null && e.AddedItems.Count > 0) { SomeHierarchyViewModel model = e.AddedItems[0] as SomeHierarchyViewModel; var obj = model.obj as TreeNode; ComBoxTreeModel treeModel = obj.Target as ComBoxTreeModel; this.equCodeTextBox.Text = treeModel.Code; if (EquConfig != null) { EquConfig.Type = treeModel.Type; } } }
/// <summary> /// 读取设备集合(Type数值:设备类型:1;设备:2) /// </summary> private void GetEquTypeAndEquTree() { string equJsonStr = JesonOperateHelper.ReadJesonFile("EquList.json"); string equTypeJsonStr = JesonOperateHelper.ReadJesonFile("EquTypeList.json"); List <Equ> equList = JsonConvert.DeserializeObject(equJsonStr, typeof(List <Equ>)) as List <Equ>; List <EquType> equTypeList = JsonConvert.DeserializeObject(equTypeJsonStr, typeof(List <EquType>)) as List <EquType>; var rootTypes = (from r in equTypeList where string.IsNullOrEmpty(r.ParentId) select r).ToList(); if (rootTypes.Count > 0) { EquType rootType = rootTypes.First();//获取根节点 ComBoxTreeModel rootNode = new ComBoxTreeModel() { Id = rootType.Id, Code = rootType.Code, Type = IoConfigParam.Type_EquType, Name = rootType.Name }; #region 加载设备信息 var equs = equList.Where(p => p.EquTypeId == rootType.Id).ToList(); ComBoxTreeModel etemp; foreach (var equ in equs) { etemp = new ComBoxTreeModel() { Id = equ.Id, Code = equ.Code, Name = equ.Name, Type = IoConfigParam.Type_Equ, Parent = rootNode }; rootNode.Child.Add(etemp); } #endregion InitTreeObjects(rootNode, rootType.Child, equList); var list = from r in rootNode.Child select new TreeNode().Parse(r, () => r.Name, () => r.Child); List <TreeNode> final = new List <TreeNode>(); TreeNode rootNode1 = new TreeNode(rootNode.Name, list) { Target = new ComBoxTreeModel() { Child = rootNode.Child, Id = rootNode.Id, Code = rootNode.Code, Name = rootNode.Name, Type = rootNode.Type, Parent = rootNode.Parent } }; final.Add(rootNode1); comboBoxItemSource = CovertToTree.CovertObjToTree(final, ref comboBoxTreeList); } }
/// <summary> /// 构造设备类型和设备树 /// </summary> /// <param name="treeNode">树节点</param> /// <param name="list">设备类型集合</param> /// <param name="equList">设备集合</param> private ComBoxTreeModel InitTreeObjects(ComBoxTreeModel treeNode, List <EquType> list, List <Equ> equList) { ComBoxTreeModel temp; ComBoxTreeModel ptemp; foreach (EquType item in list) { temp = new ComBoxTreeModel() { Id = item.Id, Code = item.Code, Name = item.Name, Type = IoConfigParam.Type_EquType }; #region 加载设备信息 var equs = equList.Where(p => p.EquTypeId == item.Id).ToList(); foreach (var p in equs) { if (p != null && !string.IsNullOrEmpty(p.Name)) { ptemp = new ComBoxTreeModel() { Id = p.Id, Code = p.Code, Name = p.Name, Type = IoConfigParam.Type_Equ, Parent = temp }; temp.Child.Add(ptemp); } } #endregion if (item.Child != null && item.Child.Count > 0) { treeNode.Child.Add(InitTreeObjects(temp, item.Child, equList)); } else { temp.Parent = treeNode; treeNode.Child.Add(temp); } } return(treeNode); }
/// <summary> /// 下拉框选择触发的事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ComboBoxTreeView_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (e.AddedItems.Count <= 0) { return; } if (this.equConfigDataGrid.SelectedItems.Count == 1) { int rowIndex = this.equConfigDataGrid.SelectedIndex; DataGridRow row = this.equConfigDataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex) as DataGridRow; if (row != null) { SomeHierarchyViewModel smodel = ((EquConfigModel)this.equConfigDataGrid.SelectedItem).SelectedEqu; if (smodel != null) { ComBoxTreeModel cbtModel = (smodel.obj as TreeNode).Target as ComBoxTreeModel; //所选行第4列的单元格数据(赋值) TextBlock tb = this.equConfigDataGrid.Columns[5].GetCellContent(row) as TextBlock; if (tb != null) { tb.Text = cbtModel.Code; } //值在界面不需要显示,故而可只给属性赋值,如果要界面显示,则此处后面注释掉的代码打开即可 (row.Item as EquConfigModel).Type = cbtModel.Type.Equals(IoConfigParam.Type_EquType) ? "设备类型" : "设备"; if (cbtModel.Type.Equals(IoConfigParam.Type_EquType)) { (row.Item as EquConfigModel).EquTypeCode = cbtModel.Code; } else { (row.Item as EquConfigModel).EquTypeCode = GetEquTypeCode(smodel); } } } } }