求WPF中遍历treeview中所有节点的方法,谢谢。是wpf中

解决方案 »

  1.   

    private TreeNode SearchNode(TreeNode td, string selectParentNum)
            {
                if (td.ToolTipText == selectParentNum)
                {
                    return td;
                }
                TreeNode targetNode = null;
                foreach (TreeNode childNodes in td.Nodes)
                {
                    targetNode = SearchNode(childNodes, selectParentNum);
                    if (targetNode != null)
                        break;
                }
                return targetNode;
            }
      

  2.   

    没做过wpf的,不过遍历树原理不是一样的吗 //在功能树中勾选指定功能集对应的节点
    private void CheckNodes(IList<String> fidList)
    {
    //检验tvw_Function是否有内容
    if (0 == tvw_Function.Nodes.Count)
    {
    Exception ex = new Exception("tvw_Function has no content!");
    StackTrace st = new StackTrace(new StackFrame(true));
    StackFrame sf = st.GetFrame(0);
    Tool.WriteLog(true , ex , st , sf);
    return;
    } TreeNode tnRoot , tnCurrent , tnCurrentPar;
    tnRoot = tvw_Function.Nodes["root"];
    tnCurrentPar = tnRoot;
    tnCurrent = tnCurrentPar.FirstNode; //遍历tvw_Function获取被勾选的节点信息
    while (tnCurrent != null && tnCurrent != tnRoot)
    {
    while (tnCurrent != null)
    {
    //若当前节点功能的fid在fidList之中,则将其勾选
    if (null != tnCurrent.Tag)
    if (null != fidList.FirstOrDefault(fid => fid == ((FunctionInfo) tnCurrent.Tag).fid))
    {
    tnCurrent.Checked = true;
    tnCurrent.ForeColor = Color.Salmon;
    } if (tnCurrent.Nodes.Count > 0)
    {
    //移动到下一层节点
    tnCurrentPar = tnCurrent;
    tnCurrent = tnCurrent.FirstNode;
    }
    else if (tnCurrent != tnCurrentPar.LastNode)
    {
    //移动到本层下一个节点
    tnCurrent = tnCurrent.NextNode;
    }
    else
    break;
    } //逐层回溯父节点
    while (tnCurrent != tnRoot && tnCurrent == tnCurrentPar.LastNode)
    {
    tnCurrent = tnCurrentPar;
    tnCurrentPar = tnCurrentPar.Parent;
    } //回溯停止后移动到本层下一个节点
    if (tnCurrent != tnRoot)
    tnCurrent = tnCurrent.NextNode;
    }
    }
      

  3.   

        WPF中展开一个TreeView控件的所有树节点
        在 Windows Form 应用中,我们碰到需要展开一个TreeView 控件的所有树节点的时候很简单,微软已经替我们提供了ExpandAll 方法,我们只要简单的一行代码tv_QTree.ExpandAll();就可以了。即 System.Windows.Forms 命名空间的 TreeView.ExpandAll 方法 。
     
        在WPF中,我们会发现,System.Windows.Controls.TreeView 中没有了 ExpandAll 方法。唯一跟展开有关系的属性和方法就是每一个TreeViewItem 中有一个属性IsExpanded 属性。这个属性定义这个节点是否打开。MSDN帮助如下:       Gets or sets whether the nested items in a TreeViewItem are expanded or collapsed. This is a dependency property.        这时候如何办呢? 很简单,自己写个递归,遍历这个树的每一个子节点,然后把每一个子节点的 IsExpanded 设置为 true.
    详情:http://ghj1976.spaces.live.com/blog/cns!F5D0FA8B5536921!212.entry