private bool IsMultiSelectCheckBoxChecked(TreeView tree, TreeViewNode node) { var treeViewItem = tree.ContainerFromNode(node) as TreeViewItem; var checkBox = FindVisualChildByName(treeViewItem, "MultiSelectCheckBox") as CheckBox; return(checkBox.IsChecked == true); }
private void SetAutomationIdForNodes(TreeView tree) { var listControl = FindVisualChildByName(this.TestTreeView, "ListControl") as TreeViewList; Stack <TreeViewNode> pendingNodes = new Stack <TreeViewNode>(); foreach (var node in tree.RootNodes) { pendingNodes.Push(node); } while (pendingNodes.Count > 0) { var currentNode = pendingNodes.Pop(); foreach (var child in currentNode.Children) { pendingNodes.Push(child); } var container = tree.ContainerFromNode(currentNode); if (container != null) { AutomationProperties.SetAutomationId(container as DependencyObject, GetNodeContent(currentNode)); (container as FrameworkElement).SetValue(FrameworkElement.NameProperty, GetNodeContent(currentNode)); } } }
/// <summary> /// 选中TreeViewNode并将其滚动到UI中间 /// </summary> /// <param name="Node">要选中的Node</param> /// <param name="View">Node所属的TreeView控件</param> /// <returns></returns> public static void SelectNode(this TreeView View, TreeViewNode Node) { if (View == null) { throw new ArgumentNullException(nameof(View), "Parameter could not be null"); } View.SelectedNode = Node; View.UpdateLayout(); if (View.ContainerFromNode(Node) is TreeViewItem Item) { Item.StartBringIntoView(new BringIntoViewOptions { AnimationDesired = true, VerticalAlignmentRatio = 0.5 }); } }
private void GetCheckBoxStates(TreeView tree) { StringBuilder sb = new StringBuilder(); Stack <TreeViewNode> pendingNodes = new Stack <TreeViewNode>(); pendingNodes.Push(tree.RootNodes[0]); while (pendingNodes.Count > 0) { var currentNode = pendingNodes.Pop(); var children = currentNode.Children; var size = children.Count; for (int i = 0; i < size; i++) { pendingNodes.Push(currentNode.Children[size - 1 - i]); } var treeViewItem = tree.ContainerFromNode(currentNode) as TreeViewItem; var checkBox = FindVisualChildByName(treeViewItem, "MultiSelectCheckBox") as CheckBox; if (checkBox.IsChecked == true) { // selected sb.Append("s|"); } else if (checkBox.IsChecked == false) { // unselected sb.Append("u|"); } else { // partial selected sb.Append("p|"); } } Results.Text = sb.ToString(); }