WPF TreeView 展开到指定节点

最近在做一个交换机管理的项目,有一个交换机的树,做树的搜索的时候 展开节点居然有点难,自己记录下来 ,以后用的到的时候可以看一下。

展开代码如下,其中

SwitchTree是treeview空间的名称,TreeNode 是绑定在树上的数据类的名称,请注意
private void ExpendTree(TreeNode node)
        {
        
            SwitchTree.UpdateLayout();
            for (int i = 0; i < SwitchTree.Items.Count; i++)
            {
                TreeNode findNd = SwitchTree.Items[i] as TreeNode;
                TreeViewItem ti = SwitchTree.ItemContainerGenerator.ContainerFromIndex(i) as TreeViewItem;
                if (findNd == node && ti != null)
                {
                    ti.IsExpanded = true;
                    ti.IsSelected = true;
                    if (ti.ItemContainerGenerator.Status != GeneratorStatus.ContainersGenerated)
                    {
                        ti.UpdateLayout();
                    }
                    //展开
                    EependTreeByNode(ti);
                    break;
                }
                else
                {
                    if (ti != null)
                    {
                       TreeViewItem treeViewItem= RecurFindeNode(node, ti);
                        if (treeViewItem == null)
                        {
                            ti.IsExpanded = false;
                            continue;
                        }
                        //展开
                        EependTreeByNode(treeViewItem);
                        break;
                    }

                }
            }
            //EependTreeByNode(node);
        }

        private TreeViewItem RecurFindeNode(TreeNode n, TreeViewItem items)
        {
            TreeViewItem tv = null;
            if (n != null && items != null && items.HasItems)
            {
                items.IsExpanded = true;
                foreach (var item in items.Items)
                {
                    var node = item as TreeNode;
                    if (node == null || (node.nodes!=null&&!node.nodes.Any()))
                    {
                        continue;
                    }
                    if (items.ItemContainerGenerator.Status != GeneratorStatus.ContainersGenerated)
                    {
                        items.UpdateLayout();
                    }
                    TreeViewItem m_item = items.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem;

                    if (n == node && m_item != null)
                    {
                        //EependTreeByNode(m_item);
                        tv = m_item;
                    }
                    else
                    {

                        TreeViewItem treeNode = RecurFindeNode(n, m_item);
                        if (treeNode != null)
                        {
                            tv = treeNode;
                            break;
                        }
                        else
                        {
                            //items.IsExpanded = false;
                        }
                    }
                }
            }

            return tv;
        }

        private void EependTreeByNode(TreeViewItem contenier)
        {

            if (contenier != null)
            {
                //if (contenier.HasItems)
                //{
                    contenier.IsExpanded = true;
                    contenier.IsSelected = true;
                //}
                //else
                //{
                //    TreeViewItem parent = (TreeViewItem)contenier.Parent;
                //    if (parent != null)
                //    {
                //        parent.IsExpanded = true;
                //        parent.IsSelected = true;
                //    }
                //}
                if (contenier.ItemContainerGenerator.Status != GeneratorStatus.ContainersGenerated)
                {

                    contenier.UpdateLayout();

                }
            }
        }

原文地址:https://www.cnblogs.com/wuyaxiansheng/p/11130939.html

时间: 2024-10-16 11:26:20

WPF TreeView 展开到指定节点的相关文章

wpf treeview 数据绑定 递归绑定节点

1.先上效果 将所有节点加入ComboBox数据源,在ComboBox中选择时下方Treeview显示该节点下的子节点. 1.xaml文件,将以下代码加入界面合适位置 1 <StackPanel> 2 <StackPanel Margin="10"> 3 <Label Content="选择组节点:"></Label> 4 <ComboBox MaxDropDownHeight="100" N

C# TreeView控件 展开指定子节点的所有父节点

通常,我们需要的功能更多的是通过展开某一个父节点同时展开所有的子节点(即:联动),但是有时候我们也需要展开某一个节点之上的所有父节点,直到当前节点,例如在删除某一个节点时,不想删除之后所有节点有折叠,这时候就需要这种功能.这样说可能不好理解,也可能是本人语言表达能力欠佳,我们来看一个图: 通常我们都是点击根节点"1"展开所有的节点,如上.但是现在我们要删除节点"11111",删除之后是自动展开到"1111",即: 注意两个图是不一样的. 如何实现

TreeView 查找指定节点

/// <summary> /// 查找指定节点 /// </summary> /// <param name="tnParent">节点</param> /// <param name="strValue">值</param> /// <returns></returns> private TreeNode FindNode(TreeNode tnParent, str

WPF TreeView 选择事件执行两次,获取TreeView的父节点的解决方法

1.TreeView选择事件执行两次 Very often, we need to execute some code in SelectedItemChanged depending on the selected TreeViewItem. ButSelectedItemChanged is called twice. This is due to stealing focus from the main window, which is screwing something up. Wha

根据treeId展开指定节点并触发单击事件

ztree.expandNode(ztree.getNodeByParam("id",treeId,null));//展开指定节点 ztree.selectNode(ztree.getNodeByParam("id",treeId,null));//选中指定节点 ztree.setting.callback.onClick(null, ztree.setting.treeId, ztree.getNodeByParam("id",treeId,n

WPF TreeView 减少节点的缩进

WPF TreeView 减少节点的缩进 [服务器破旧,速度较慢,见谅!] 原文地址:https://www.cnblogs.com/swack/p/11363658.html

WPF TreeView HierarchicalDataTemplate

原文 WPF TreeView HierarchicalDataTemplate   <StackPanel Margin="0,0,0,0"> <StackPanel.Resources> <HierarchicalDataTemplate x:Key="MonTemp" DataType = "{x:Type local:MonthViewModel}" ItemsSource = "{Binding

在空数组中填充指定节点

page({ data:{ list:[] }, //监听显示 onshow:function(){         //到前台显示 var that = this; hotapp.searchkey({                //后台搜索钥匙 "prefix":"item", //前缀节点 "pageIndex":pageIndex, //当前页码 "pagesize":100    //页面大小 }, functi

读取XML文件的指定节点的值 并转换为Item

cmb_State_Send.ItemsSource = null; XmlDocument doc = new XmlDocument(); doc.Load("D:\\模板\\WorkstationState_Config.xml"); //加载Xml文件 XmlElement rootElem = doc.DocumentElement; //获取根节点 XmlNode xn = rootElem.SelectSingleNode("//Workstation[@Nam