locked
How to subclass TreeNode and use it with TreeView? RRS feed

  • Question

  • User260076833 posted

    Hello,

    I need to subclass TreeNode to extend it with new attributes and use it in a custom control.

    public class MyTreeNode : TreeNode
    {
     public String type { get; set; }
    }
    

    I add them to a TreeView like this:

    public class TreePageView : CompositeControl 
    {
     TreeView trv;
    
     ... 
    
     protected override void CreateChildControls()
     {
      ...
      trv.Nodes.Clear();
      MyTreeNode t = new MyTreeNode();
      t.myAttribute = "XXX";
      trv.Nodes.Add(t);
     }
    }

    I can verify in debug mode that the trv.Nodes collection contains an instance of the class MyTreeNode.

    However, in the SelectedNodeChanged event we have another situation:

    protected void tvw_SelectedNodeChanged(object snd, EventArgs evt)
    {
     if (!(trv.SelectedNode is MyTreeNode))
      return;
     ... // never reached
    }
      

    Allthough I only add subclassed tree nodes (instances of class MyTreeNode) to the TreeView, the selected node only is an instance of the TreeNode class, but not of the MyTreeNode class.

    But in the SelectedNodeChanged event I need access to the originally added node which is an instance of MyTreeNode.

    How can I solve that?

    Thanks
    Magnus

    Wednesday, December 7, 2016 5:37 PM

All replies

  • User260076833 posted

    What's wrong with this question? Is it the wrong forum? Is the problem not described sufficiently?

    I believe custom controls are a very nice thing, and I would like to have a place where you can talk about it...

    Thanks
    Magnus

    Friday, December 9, 2016 4:14 PM