Reputation: 483
I have an object which I am using to hold output data from a simulation. I'd like to populate a TreeView with the public properties of this object, but since I feel that I may add or remove members from this object, it makes sense to me to "bind" the object in some way such that the TreeView logic doesn't need to be manually updated with the object's properties.
As an example, let's say I have a class called Temperature
, as follows:
public class Temperature
{
public double Wall { get; set; }
public double Floor { get; set; }
public Temperature()
{
Wall = 0d;
Floor = 0d;
}
}
So I would like the TreeView to have a root node named Temperature, followed by child nodes named Wall and Floor. My current understanding would be to just hard code it, e.g.:
TreeNode tnTemperatureParent = treeViewData.Nodes.Add("Temperature");
tnTemperatureParent.Nodes.Add("Wall");
tnTemperatureParent.Nodes.Add("Floor");
Now, let's say I have to add a public property named Ceiling
to the Temperature
class. I would have to go to my TreeView logic and add a line to include a child node for Ceiling
.
This all seems well and good for small datasets and primitive datatypes, but I need a higher degree of control here. The objects I wish to populate the TreeView with will end up holding more information, such as lists which I would like to use to plot. I'm looking at something around 50+ output variables, and it may be decided before completion that we want a few more, or a few less.
So, in summary, I suppose I have two questions here:
A) What is the optimal way to populate a TreeView with the public properties of an object?
B) Should I store the data as part of the TreeNodes (e.g. using the Tag
property) or should I have the tree structure reference the object in parallel?
EDIT: I'm still fairly new at the whole object-oriented paradigm (started as a procedural programmer), so I have a hard time visualizing at times. Here's some additional information that may help understand the direction I'm going in.
During a simulation I've written, I hold time-step quantities in an "output data" class. The class is broken into subclasses (e.g. temperature, pressure), each which have a few parameters (e.g. wall, floor, ceiling in the example above). The reason I'd like to dump them into a TreeView is so that I can configure drag-drop functionality, allowing me to drag a data parameter (e.g. Floor Temperature) onto another control (such as a plot, or a table) such that the data can be presented. I hope that helps.
Upvotes: 3
Views: 3873
Reputation: 25895
For this kind of behavior in Windows Forms, I'd recommend that you use the PropertyGrid control:
(source: microsoft.com)
(Source: Getting the Most Out of the .NET Framework PropertyGrid Control)
All you need to do is set the SelectedObject
property to your custom object and the PropertyGrid will automatically discover your class' properties. It also supports hierarchical objects and editing. Some types, such as Colors, have built-in editors (as depicted above).
The TreeView is not well suited for this kind of automatic binding... though you could write your own code to perform the binding for you using reflection.
I've not tried myself, but I beleive that the PropertyGrid's support for Drag'n'Drop is lacking, as evidenced by this post:
C# PropertyGrid drag drop
Upvotes: 1