简体   繁体   English

如何在Telerik RadTreeView中设置SelectedNode和设置选中节点的Focus?

[英]How to set the SelectedNode and Set the Focus of the selected node in Telerik RadTreeView?

I am using the Telerik RadTreeView with ASP.Net C#. I am able to set the Selected Node using the following code:我将 Telerik RadTreeView 与 ASP.Net C# 一起使用。我可以使用以下代码设置选定节点:

        var node = radTreeViewMenuStructure.Nodes.FindNodeByValue(linkID.ToString());

        if (node != null) // <- equals null when not on the root of the tree
        {
            node.Selected = true;
            node.Expanded = true;
            node.ExpandParentNodes();
            node.Focus();
        }

The above code sets the selected node only if the node is just off the root and not enclosed in a parent node.上面的代码仅在节点刚好位于根之外且未包含在父节点中时才设置选定节点。 My node = null when choosing an ID of a node that is enclosed within a parent node. My node = null when choosing an ID of a node that is enclosed within a parent node. Any suggestions?有什么建议么?

The.FindNodeByValue looks in the Nodes of the tree view. The.FindNodeByValue 在树视图的节点中查找。 It doesn't look at each child node.它不查看每个子节点。 The solution was to recursively walk tree.解决方案是递归地遍历树。 Here is my code that finally solved the issue:这是我最终解决问题的代码:

    private void SelectLink(int linkID, RadTreeNodeCollection rootNodes)
    {
        var node = rootNodes.FindNodeByValue(linkID.ToString());
        if (node != null)
        {
            node.Selected = true;
            node.Expanded = true;
            node.ExpandParentNodes();
            node.Focus();

            ... Do some other work ...

            return;
        }

        // for each node with children  
        foreach (RadTreeNode item in rootNodes.Cast<RadTreeNode>().Where(item => item.Nodes.Count > 0))
        {
            // Recursive call to self to walk the tree
            SelectLink(linkID, item.Nodes);
        }
    }

I then simply call the method with the root RadTreeView:然后,我只需使用根 RadTreeView 调用该方法:

SelectLink(radTreeViewMenuStructure.Nodes, idToFind);

You just need to also call node.ExpandParentNodes();您只需要调用node.ExpandParentNodes();

You just need to do: radTreeViewMenuStructure.FindNodeByValue() that will serch the whole tree.你只需要做: radTreeViewMenuStructure.FindNodeByValue() 这将搜索整棵树。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM