public static WpfTreeItem 择行(this WpfTree tree, int rowIndex) { var item = tree.Find <WpfTreeItem>(); item.SearchProperties[WpfTreeItem.PropertyNames.Instance] = (rowIndex + 1).ToString(); item.SearchConfigurations.Add(SearchConfiguration.ExpandWhileSearching); item.Selected = true; return(item); }
/// <summary> /// /// </summary> /// <param name="tree"></param> /// <param name="path">使用 / 作为分隔符,如果数据中有此字符,则可以使用 $ 来分隔。</param> /// <returns></returns> public static WpfTreeItem 行(this WpfTree tree, string path) { string[] pathes = null; if (path.Contains("$") && path.Contains("/")) { pathes = path.SplitBy("$"); } else { pathes = path.SplitBy("/"); } if (pathes.Length > 0) { tree.Find(); WpfControl parent = tree; for (int i = 0, c = pathes.Length; i < c; i++) { var title = pathes[i]; //方案一 //var nodes = GetNodes(parent); //for (int j = 0, c2 = nodes.Count; j < c2; j++) //{ // var node = nodes[j] as WpfTreeItem; // if (node.Name == title) // { // if (i < c - 1) { node.Expanded = true; } // parent = node; // break; // } //} //方案二 //var node = parent.Find<WpfTreeItem>(title); //node.SearchConfigurations.Add(SearchConfiguration.ExpandWhileSearching); //node.Find(); //if (i < c - 1) { node.Expanded = true; } //parent = node; //方案三 var node = parent.Find <WpfTreeItem>(title); if (i < c - 1) { //由于 WinUIA 的速度太快,需要一定的时间来等待控件生成。 Thread.Sleep(100); var pattern = (node.NativeElement as WinUIA.AutomationElement) .GetCurrentPattern(WinUIA.ExpandCollapsePattern.Pattern) as WinUIA.ExpandCollapsePattern; pattern.Expand(); } parent = node; } if (parent is WpfTreeItem) { return(parent as WpfTreeItem); } } throw new UIAutomationException("没有找到对应的行:" + path); }