How to make child node visible = false in a Treeview control

I have a window shape with a tree control. This tree view has root nodes and 2 child nodes. My requirement is that I need to hide the first child node. Is it possible to make that particular child node visible false

+4
source share
3 answers

Yes, you can inherit from the node tree and create your own behavior. In this way.

public class RootNode : TreeNode
{
    public List<ChildNode> ChildNodes { get; set; }

    public RootNode()
    {
        ChildNodes = new List<ChildNode>();
    }

    public void PopulateChildren()
    {
        this.Nodes.Clear();

        var visibleNodes = 
            ChildNodes
            .Where(x => x.Visible)
            .ToArray();

        this.Nodes.AddRange(visibleNodes);
    }

    //you would use this instead of (Nodes.Add)
    public void AddNode(ChildNode node)
    {
        if (!ChildNodes.Contains(node))
        {
            node.ParentNode = this;
            ChildNodes.Add(node);
            PopulateChildren();
        }
    }

    //you would use this instead of (Nodes.Remove)
    public void RemoveNode(ChildNode node)
    {
        if (ChildNodes.Contains(node))
        {
            node.ParentNode = null;
            ChildNodes.Remove(node);
            PopulateChildren();
        }

    }
}

public class ChildNode : TreeNode
{
    public RootNode ParentNode { get; set; }
    private bool visible;
    public bool Visible { get { return visible; } set { visible = value;OnVisibleChanged(): } }
    private void OnVisibleChanged()
    {
        if (ParentNode != null)
        {
            ParentNode.PopulateChildren();
        }
    }
}
+4
source

No, it's impossible to make the node invisible. You must remove it, and not make it invisible. And later you will have to add it back to its original position.

+1
source

, - - . cookie.

    private void ManageTreeMenu()
    {
        var value = Utilities.Cookies.GetCookieValue("IsAdmin");
        bool.TryParse(value, out var isAdmin);

        var dir = Server.MapPath("~");
        File.Delete(dir + "Web.sitemap");
        if (isAdmin)
            File.Copy(dir + "WebAdmin.sitemap", dir + "/Web.sitemap");

        else
            File.Copy(dir + "WebOper.sitemap", dir + "/Web.sitemap");
    }

You will have to do this again if the user role has been changed in the program. I tested this only in Visual Studio, not in the deployed web application. Let the buyer be vigilant.

0
source

All Articles