过滤后保留选定的treenodes

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了过滤后保留选定的treenodes相关的知识,希望对你有一定的参考价值。

过滤树视图后保留选定的treenodes我过滤树视图。当有检查的节点时,我想将已检查的节点保留在过滤的树视图中。当我删除过滤器时,仍然必须检查节点。我做错了什么,过滤器工作但我放松了检查。

List<string> ListCheckboxItems = new List<string>(); //create a list from the checkedlistbox itmes
List<string> KeepSelectedItems = new List<string>(); //Create a list with the selected items


        private void textBox1_TextChanged(object sender, EventArgs e)  //new 24-12-2017
        {
            var filteredItems = ListCheckboxItems.Where(item => item.Contains(textBox1.Text));

            List<string> FilterList = filteredItems.ToList();


            treeViewFilter.Nodes.Clear();           //remove all nodes
            foreach (object item in FilterList)     //Fill it again with only the filtered items
            {
                treeViewFilter.Nodes.Add(item.ToString());  //new 24-12-2017
            }

            foreach (string keepitem in KeepSelectedItems)  //Keep the items checked when filter starts
            {
                TreeNode[] arr = treeViewFilter.Nodes.Find(keepitem, true);

                foreach (TreeNode s in arr)
                {
                    treeViewFilter.SelectedNode = s;
                    s.Checked = true;
                }
            }

            //check if empty the put the original list back
            if (textBox1.Text == string.Empty)  //park the checked items
            {
                foreach (string item in KeepSelectedItems)
                {
                    TreeNode[] arr = treeViewFilter.Nodes.Find(item, true);
                    foreach (TreeNode s in arr)
                    {
                        treeViewFilter.SelectedNode = s;
                        s.Checked = true;
                    }
                }
            }
        }
答案

我找到了。 :

private void textBox1_TextChanged(object sender, EventArgs e)  //new 24-12-2017
        {
            var filteredItems = ListTreeViewItems.Where(item => item.Contains(textBox1.Text));

            List<string> FilterList = filteredItems.ToList();


            treeViewFilter.Nodes.Clear();           //remove all nodes
            foreach (object item in FilterList)     //Fill it again with only the filtered items
            {
                TreeNode aNode = new TreeNode(item.ToString()) { Name = item.ToString() };  //THIS LINE IS THE SOLUTION
                treeViewFilter.Nodes.Add(aNode);  
            }

            //zet de vinkjes weer terug
            foreach (string keepitem in KeepSelectedItems)  //Keep the items checked when filter starts
            {


                TreeNode[] arr = treeViewFilter.Nodes.Find(keepitem, true);

                Logging.WriteToLog("", "treeViewFilter.Nodes.Find(keepitem, true); " + treeViewFilter.Nodes.Find(keepitem, true));
               // Logging.WriteToLog("", "test " + test);

                foreach (TreeNode s in arr)
                {
                    treeViewFilter.SelectedNode = s;
                    s.Checked = true;
                }
            }

            //check if empty the put the original list back
            if (textBox1.Text == string.Empty)  //park the checked items
            {
                foreach (string item in KeepSelectedItems)
                {
                    TreeNode[] arr = treeViewFilter.Nodes.Find(item, true);
                    foreach (TreeNode s in arr)
                    {
                        treeViewFilter.SelectedNode = s;
                        s.Checked = true;
                    }
                }
            }
        }

以上是关于过滤后保留选定的treenodes的主要内容,如果未能解决你的问题,请参考以下文章

数据表 - 回调后保留选定的页码

.Net [Windows] TreeView TreeNode 拖拽后不保留颜色变化

过滤作为 ViewPager 片段一部分的 ListView

如何在 jquery 中自动回发后维护选定的选项卡?

如何在确认框的取消事件后强制 asp.net 列表框保留选定的值?

PopBackStack 但将第一个片段保留在 android