![](/img/trans.png)
[英]Is there a property of Telerik RadTreeView to collapse all children when a parent is collapsed?
[英]Get the all relationships including all Children of children, collapsed up to the point of the child(or parent) that contains the keyword
我有一個 treeview,它基於目錄結構填充了多個節點,有時是單個節點,但通常是父子關系。
我的目標是按關鍵字過濾 treeview。
我完成了這個使用
private void Filter()
{
TreeView tv = new TreeView();
//Clone backup List<TreeNode> myTreeview into temporary TreeView tv
foreach (TreeNode n in myTreeview)
tv.Nodes.Add((TreeNode)n.Clone());
treeViewInForm.Nodes.Clear();
if (txtKeyword.Text == "")
{
treeViewInForm.Nodes.AddRange(myTreeview.ToArray());
}
else
{
foreach (TreeNode n in tv.Nodes)
{
RecursiveFunction(n);
}
treeViewInForm.ExpandAll();
}
}
private void RecursiveFunction(TreeNode treeNode)
{
if (treeNode.Text.ToLower().Contains(txtKeyword.Text.ToLower()))
{
this.Invoke((MethodInvoker)delegate
{
TreeNode root = new TreeNode();
TreeNode node = root;
bool beginn = true;
foreach (string pathBits in treeNode.FullPath.Split('\\'))
{
//Comnsole.WriteLine("pb: " + pathBits);
if (beginn)
{
beginn = false;
node.Text = pathBits;
}
else
{
node = AddNode(node, pathBits);
}
}
treeViewInForm.Nodes.Add(root);
});
}
foreach (TreeNode tn in treeNode.Nodes)
{
RecursiveFunction(tn);
}
}
假設關系是示例 A
Node
Child(Contains Keyword)
ChildOfChild(Does or does not contain Keyword)
...
我得到的是示例 B
Node
Child(Contains Keyword)
或示例 C,如果ChildOfChild
包含關鍵字
Node
Child(Contains Keyword)
Node
Child(Contains Keyword)
ChildOfChild(Does contain Keyword)
如何獲得示例 A,折疊到包含關鍵字的第一個子(或父)的點?
您可以采用Filter TreeView 中發布的解決方案,其中包含所有節點和子節點,它提供了一種更好更快的過濾TreeView
的方法。 挑選(刪除)不匹配的,而不是創建一棵新樹。 但是,我發現該代碼中可能存在錯誤,或者某些東西可能適合該問題。 我不知道,會員已經不在附近驗證了。
無論如何,基於這個想法,這是一個建議的版本。
// +
using System.Text.RegularExpressions;
// ...
private readonly List<TreeNode> backupNodes = new List<TreeNode>();
private void SomeCaller()
{
// Clear and rebuild this list whenever the tree changes.
if (!backupNodes.Any())
{
backupNodes.AddRange(treeViewInForm.Nodes
.Cast<TreeNode>()
.Select(n => n.Clone() as TreeNode));
}
treeViewInForm.Nodes.Clear();
treeViewInForm.Nodes.AddRange(backupNodes
.Select(n => n.Clone() as TreeNode)
.ToArray());
if (txtKeyword.Text.Trim().Length == 0) return;
Filter(treeViewInForm, txtKeyword.Text);
}
private void Filter(TreeView tv, string keyword)
{
IEnumerable<TreeNode> GetAllNodes(TreeNodeCollection Nodes)
{
foreach (TreeNode tn in Nodes)
{
yield return tn;
foreach (TreeNode child in GetAllNodes(tn.Nodes))
yield return child;
}
}
tv.BeginUpdate();
// .Reverse() to process the deepest nodes first...
foreach (var node in GetAllNodes(tv.Nodes).Reverse())
{
if (!Regex.IsMatch(node.Text, keyword, RegexOptions.IgnoreCase))
{
// This `if` is missing in the original code.
// Without it, a non-match parent will be removed
// even if it has child matches at any level.
if (node.Nodes.Count == 0)
{
if (node.Parent != null)
node.Parent.Nodes.Remove(node);
else
tv.Nodes.Remove(node);
}
}
}
tv.ExpandAll();
tv.EndUpdate();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.