Tree widgets are frequently used. The following two functions are often required for property controls:
1. Treelist has a checkbox, And the node must have three States (all subnodes are selected, all subnodes are not selected, and some subnodes are selected ). This function can be easily implemented using the devxpress treelist control.
Set treelist. optionsview. showcheckboxes =True//Whether to display checkbox
Set treelist. optionsbehavior. allowindeterminatecheckstate =True;//Sets whether a node has an intermediate state. That is, some subnodes are selected, and some subnodes are not selected.
After these two attributes are set, the treelist has a checkbox and the node has three states.
2. Select the function that the parent or child nodes affect each other. For example, select the parent node to select all child nodes. Bind two events of treelist: afterchecknode and beforechecknode
Implemented FunctionsCodeAs follows:
Private VoidTreelist1_afterchecknode (ObjectSender, devexpress. xtratreelist. nodeeventargs E)
{
Setcheckedchildnodes (E. node, E. node. checkstate );
Setcheckedparentnodes (E. node, E. node. checkstate );
}
Private VoidTreelistpolicbeforechecknode (ObjectSender, devexpress. xtratreelist. checknodeeventargs E)
{
E. State = (E. prevstate = checkstate. checked? Checkstate. Unchecked: checkstate. Checked );
}
/// <Summary>
/// Set the subnode status
/// </Summary>
/// <Param name = "Node"> </param>
/// <Param name = "check"> </param>
Private Void Setcheckedchildnodes (treelistnode node, checkstate check)
{
For ( Int I = 0 ; I <node. nodes. Count; I ++)
{
Node. nodes [I]. checkstate = check;
Setcheckedchildnodes (node. nodes [I], check );
}
}
/// <Summary>
/// Set the status of the parent node
/// </Summary>
/// <Param name = "Node"> </param>
/// <Param name = "check"> </param>
Private Void Setcheckedparentnodes (treelistnode node, checkstate check)
{
If (Node. parentnode! = Null )
{
Bool B =False ;
Checkstate state;
For ( Int I = 0 ; I <node. parentnode. nodes. Count; I ++)
{
State = (checkstate) node. parentnode. nodes [I]. checkstate;
If (! Check. Equals (State ))
{
B =! B;
Break ;
}
}
Node. parentnode. checkstate = B? Checkstate. Indeterminate: check;
Setcheckedparentnodes (node. parentnode, check );
}
}
1. Treelist. nodecellstyle event
Node display (including node display due to window switching) and status change will trigger this event. This event is mainly used to change the node display style.
Private Void Treelist1_nodecellstyle ( Object Sender, devexpress. xtratreelist. getcustomnodecellstyleeventargs E)
{
If (E. node. checkstate = checkstate. Unchecked)
{
E. Appearance. font = New Font (devexpress. utils. appearanceobject. defaultfont, fontstyle. Strikeout );
E. Appearance. forecolor = color. Gray;
}
}
The above code implements the following: the words of the node whose checkstate is unchecked carry a hyphen and the background is gray.
2. Treelist. DoubleClick event
Double-click the node to trigger the event. Note that you must double-click the node to trigger the event when treelist. optionsbehavior. editable = false.
Private Void Treelist1_doubleclick ( Object Sender, eventargs E)
{
Treelistnode clickednode = This . Treelist1.focusednode;
String Displaytext = clickednode. getdisplaytext ( " Test " );
MessageBox. Show ( " You clicked " + Displaytext );
}
3. Treelist hit test features
Private Void Treelist1_mousemove ( Object Sender, mouseeventargs E)
{
Point point = treelist1.pointtoclient (cursor. position );
Treelisthitinfo hitinfo = treelist1.calchitinfo (point );
Switch (Hitinfo. hitinfotype)
{
Case Hitinfotype. Cell:
This . Cursor = cursors. hand;
Break ;
Case Hitinfotype. nodecheckbox:
This . Cursor = cursors. paneast;
Break ;
Default :
This . Cursor = cursors. default;
Break ;
}
}