/// <summary> /// 递归向下查找 /// </summary> TreeNodeEntityViewModel FindDownward(List <TreeNodeEntityViewModel> nodes, string id) { if (nodes == null) { return(null); } for (int i = 0; i < nodes.Count; i++) { if (nodes[i].ID == id) { return(nodes[i]); } TreeNodeEntityViewModel node = FindDownward(nodes[i].Nodes, id); if (node != null) { return(node); } } return(null); }
public void LoadTyeEncodeCheckDevice(List <TreeNodeEntity> uses) { ObservableCollection <TreeNodeEntityViewModel> result = new ObservableCollection <TreeNodeEntityViewModel>(); var all = new TreeNodeEntityViewModel(new TreeNodeEntity() { Name = "全部" }); all.IsSelected = true; result.Add(all); foreach (var item in uses) { result.Add(new TreeNodeEntityViewModel(item)); } this.EntityCheck = result; this.SelectEntityNode = all; }
public void RefreshNodes(string text, TreeNodeEntityViewModel selectNode) { if (selectNode == null) { return; } matchNodes.Clear(); int code; bool isCode = text != null && text.StartsWith("0") && int.TryParse(text.Trim('0'), out code) || text == "0"; Predicate <string> strMatch = l => { if (l == null) { return(false); } if (string.IsNullOrEmpty(text)) { return(true); } return(l.Contains(text)); }; Predicate <string> codeMatch = l => { if (l == null) { return(false); } if (string.IsNullOrEmpty(text)) { return(true); } return(l.StartsWith(text.TrimEnd('0'))); }; Predicate <TreeNodeEntityViewModel> match = l => { bool result = isCode ? codeMatch(l.Code) : strMatch(l.Name) || strMatch(l.NamePY); if (result) { matchNodes.Add(l); } return(result); }; Action <List <TreeNodeEntityViewModel> > action = null; action = k => { foreach (var item in k) { //item.Visibility = Visibility.Visible; // Do:前序遍历 if (item.Nodes.Count > 0) { action(item.Nodes); } item.Visibility = match(item) ? Visibility.Visible : Visibility.Collapsed; // Do:检查子项是否有显示的 if (item.Nodes.Count > 0) { item.Visibility = item.Nodes.Exists(l => l.Visibility == Visibility.Visible) ? Visibility.Visible : Visibility.Collapsed; } } }; foreach (var item in this.Nodes) { // Do:先检查第一级别过滤设备节点 item.Visibility = selectNode.Name == "全部" || selectNode.Name == item.Name ? item.Visibility = Visibility.Visible : item.Visibility = Visibility.Collapsed; if (item.Visibility == Visibility.Collapsed) { continue; } item.IsExpanded = selectNode.Name == item.Name; // Do:递归检查下面子节点,只检查是否匹配 action(item.Nodes); // Do:当前项匹配或者有匹配的子项时显示 item.Visibility = match(item) || item.Nodes.Exists(l => l.Visibility == Visibility.Visible) ? Visibility.Visible : Visibility.Collapsed; } //// Do:只有一项匹配 //if (matchNodes.Count == 1) //{ // Action<TyeEncodeDeviceEntityNode> ExpandParent = null; // ExpandParent = l => // { // l.IsExpanded = true; // if (l.Parent != null) // { // ExpandParent(l.Parent); // } // }; // ExpandParent(matchNodes.First()); // matchNodes.First().IsSelected = true; //} // Message:目前需求匹配项都展开 2019-03-23 if (!string.IsNullOrEmpty(text) && matchNodes.Count > 0) { Action <TreeNodeEntityViewModel> ExpandParent = null; ExpandParent = l => { l.IsExpanded = true; if (l.Parent != null) { ExpandParent(l.Parent); } }; Action <TreeNodeEntityViewModel> CloseChildChild = l => { if (l.Nodes == null) { return; } foreach (var item in l.Nodes) { if (item.Nodes == null) { return; } foreach (var item1 in item.Nodes) { item1.IsExpanded = false; } } }; if (isCode) { var find = matchNodes.Find(l => l.Code == text.TrimEnd('0')); if (find != null) { ExpandParent(find); find.IsSelected = true; find.IsExpanded = true; CloseChildChild(find); } } else { var collection = matchNodes.Take(500); foreach (var item in matchNodes) { ExpandParent(item); } // Message:只有一项匹配展开 if (matchNodes.Count == 1) { matchNodes.First().IsSelected = true; } } } this.RefreshCount(); }