Flexible TreeView has these ways to control the node control visibility inside a node:
Visible node control property;
Node control filtration;
Visibility and VisibilityManager node control properties;
IsVisibleForNode node control method.
Read about these modes below and choose which one is appropriate for you.
Visible property
You can control the node control`s visibility using the Visible property (enabled by default). If the node control is not visible (Visible=false), all other visibility`s settings do not taken into account!
Node control filtration
By default, every node control in the treeview is visible in every node, but you can control the node control`s visibility using the node control filtration. The node control filtration mode is useful when you definitely know which node control for which node to show or hide (static mode), or if you want to make the decision dynamically (dynamic mode). To do that, adjust one of the filter modes to the Options.NodeControl.NodeControlFilterMode treeview property and decide for which nodes to hide node controls as stated below. Available filter modes:
Disabled – show all node controls within every node;
Static – filter`s settings are stored in the NodeControlFilter treeview property;
Dynamic – filter`s settings are dynamically requested each time through the FilterNodeControl treeview event.
The node control filtration mode is useful when you definitely know which node control for which node to show or hide (static mode), or if you want to make the decision dynamically (dynamic mode).
Static filtration
If you know what node control to hide for which nodes, then use static node control filtration, which is the fastest way to filter. To enable it, set the Options.NodeControl.NodeControlFilterMode treeview propery to eNodeControlFilterMode.Static value and use the indexer of the NodeControlFilter treeview property to set the node control`s visibility for particular nodes. Availlable visibility modes:
Visible – node control is always visible within the specified node;
Hidden – node control is hidden within the specified node;
VisibleForThisNodeOnly – node control is visible only within the specified node. Note that you can apply this mode for many nodes and node control will be visible only for all those nodes!
Example:
Here we`ll hide the image node control in the child node while show it in the root node.
// add two node controls to show an image and text.
NodeImage img =new NodeImage();
img.AttachTo(tree);
NodeTextBox tb =new NodeTextBox();
tb.AttachTo(tree);// add parent and child nodes.
NodeWithImage root =new NodeWithImage("Root node", Resources.SomeImage);
root.AttachTo(tree);// bind the same image to the child node too to demonstrate that it will be hidden by visibility settings.
NodeWithImage child =new NodeWithImage("Child node", Resources.SomeImage);
child.AttachTo(root);// hide the image node control in the child node.
tree.NodeControlFilter[child, img]= eNodeControlVisibility.Hidden;// enable static visibility filtration.
tree.Options.NodeControl.NodeControlFilterMode= eNodeControlFilterMode.Static;
Dynamic filtration
If you don`t know for which nodes to hide node controls, then you can use dynamic filtration. To do that, set the Options.NodeControl.NodeControlFilterMode treeview property to eNodeControlFilterMode.Dynamic value and subscribe to the FilterNodeControl treeview event, where you can then decide whether to show a specified node control inside a specified node.
Example:
// add node controls to show an image and text.
NodeImage img =new NodeImage();
img.AttachTo(tree);
NodeTextBox tb =new NodeTextBox();
tb.AttachTo(tree);// add parent and child nodes.
NodeWithImage root =new NodeWithImage("Root node", Resources.SomeImage);
root.AttachTo(tree);
NodeWithImage child =new NodeWithImage("Child node", Resources.SomeImage);
child.AttachTo(root);// activate dynamic node control filtration.
tree.Options.NodeControl.NodeControlFilterMode= eNodeControlFilterMode.Dynamic;// subscribe to the event where we`ll decide whether to show the node control inside the node.
tree.FilterNodeControl+= tree_FilterNodeControl;void tree_FilterNodeControl(FlexibleTreeView pTreeview, FilterNodeControlEventArgs pArgs){// hide the image node control in the child node.if(pArgs.Node.Text=="Child node"&& pArgs.NodeControlis NodeImage){
pArgs.ControlVisibility= eNodeControlVisibility.Hidden;}}
Visibility and VisibilityManager
Generally, the node control`s visibility is controlled by the Visible node control property. So, you can either show or hide the node control. Also, you can control the node control`s visibility using the node control filtration feature. But all those methods do not take into account a node`s state for which the node control should be shown. For instance, if you want to display a particular node control only for selected nodes. To solve that, node control provides a way to define a node`s states (selected, focused, etc.) for which to display it in the node. To define these states, use the Visibility and VisibilityManager node control properties. Note that you can define more than one state combining the eObjectVisibility enum items.
The Visibility property allows you to define a node`s states for every node control separately.
Example:
NodeTextBox tb =new NodeTextBox();// show this node control for selected nodes and general (not selected, not focused, etc.) nodes.
tb.Visibility= eObjectVisibility.SelectedNode| eObjectVisibility.GeneralNode;
tb.AttachTo(tree);
NodeTextBox tb =new NodeTextBox();// show this node control only for the selected or under the mouse cursor node.
tb.Visibility= eObjectVisibility.SelectedNode| eObjectVisibility.HotNode;
tb.AttachTo(tree);
The VisibilityManager allows you to control many node controls` visibility from one point as ControlContainer does.
Example:
// create the visibility manager.
ObjectVisibilityManager visManager =new ObjectVisibilityManager();// create the first node control.
NodeTextBox ctrl1 =new NodeTextBox();// attach the visibility manager.
ctrl1.VisibilityManager= visManager;
ctrl1.AttachTo(tree);// create the second node control.
NodeImage ctrl2 =new NodeImage();// attach the visibility manager.
ctrl2.VisibilityManager= visManager;
ctrl2.AttachTo(tree);// show all bound node controls only in the focused node.
visManager.Visibility= eObjectVisibility.FocusedNode;// temporary disable the visibility manager and use the // nodeControl.Visibility property value instead.
visManager.Enabled= false;
ctrl1.Visibility= eObjectVisibility.FocusedNode;
ctrl2.Visibility= eObjectVisibility.HotNode;
IsVisibleForNode method
If you need to manually control the node control visibility for every single node, you can override the IsVisibleForNode node control method.
Note that to tell the treeview to use this method, you need to enable the node control filtration (either the static or dynamic mode. Static mode is preferable.) using the NodeControlFilterMode treeview property!
You should return from the IsVisibleForNode method one of these values:
0 – the node control is not visible in the specified node;
1 – the node control is visible in the specified node;
-1 – do not use this method`s results and check other settings.
Note that the result of this method has priority over all other settings if it returns 0 or 1 values.
To manually control the node control visibility, derive a new node control class from the built-in one and override the IsVisibleForNode method as stated below:
// declare new node control.class NodeTextBoxEx : NodeTextBox
{protectedoverrideint IsVisibleForNode(Node pNode){// display this node control only for nodes with children.if(pNode.HasChildren){return1;}return-1;}}// use our node control.
NodeTextBoxEx tb =new NodeTextBoxEx();
tb.AttachTo(tree);
Note that in this case you can`t add this node control through the Visual Studio designer, you need to add manually in the code.
Which method to use?
So which method do you use to control the node control visibility? Follow these rules:
if you want to hide the node control and do not use other visibility settings, disable the Visible property;
if you already know for which nodes the node control will be visible or invisible and don`t have many nodes, use the static node control filtration mode. Don`t use it when you need to change the node control visibility for many nodes as it may hurt performance;
if you don`t know for which nodes the node control will be visible, or if you want to dynamically decide the node control visibility without much additional coding, use the dynamic node control filtration mode;
if the node control visibility depends on the node`s state, use the Visibility node control property;
if you have complex rules of node control visibility or want to control it manually, use the IsVisibleForNode node control method. This way is optimal for a treeview with many nodes and is very fast.
Flexible TreeView v3.3 maintenance release has been released. Separate assemblies for .NET 2.0, 4.0 and 4.0 Client Profile, HTML markup extension, etc.