过滤树视图后保留选定的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;
}
}
}
}
答案 0 :(得分:0)
我找到了。 :
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;
}
}
}
}