Introduction
In this article I am going to explain how you can link a treenode in a treeview to your underlying model.
I can't see the bush with the trees in front of it
Linking a TreeNode to your document structure in an application is common problem. There's almost always an extensive amount of code needed to map all the treenodes to the appropiate items in your code. Well here comes a simple solution to the problem:
Making a custom TreeNode
first we need to make a new class and inherit the original treenode. The code looks like this:
public class WMIClassNode: TreeNode
{
public WMIClassNode()
{
}
private ManagementObject obj;
public ManagementObject ManagementObject { get { return obj; } set { obj = value; } }
}
I use this simple class to link a treenode to a ManagementObject in my application. No need for all that difficult code, just use the ManagementObject property of this class to get the ManagementObject associated with this node.
Using the customized TreeNode
You can use this TreeNode just as good as any other TreeNode. You can simple make new instances of the TreeNode and add them to the treeview. The extra property ManagementObject can be used to link this node to a ManagementObject in your application.
Conclusion
I hope this article explains a little how to customize the standard .Net components for your own use.