private void FormProjectClear_Load(object sender, EventArgs e)
{
//是否显示CheckBox
this.treeList1.OptionsView.ShowCheckBoxes = true;
//设置节点是否有中间状态,即一部分子节点选中,一部分子节点没有选中
this.treeList1.OptionsBehavior.AllowIndeterminateCheckState = true;
}
在load里面配置上checkBox的形态
每次对数据源进行绑定之后,都要结合一个刷新,如
this.treeList1.DataSource = item.Items;
this.treeList1.RefreshDataSource();
节点勾选设置代码,放在form的代码里面
/// <summary>
/// 节点选中前事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void treeList1_BeforeCheckNode(object sender, DevExpress.XtraTreeList.CheckNodeEventArgs e)
{
e.State = (e.PrevState == CheckState.Checked ? CheckState.Unchecked : CheckState.Checked);
}
/// <summary>
/// 节点选中后事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void treeList1_AfterCheckNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e)
{
SetCheckedChildNodes(e.Node, e.Node.CheckState);
SetCheckedParentNodes(e.Node, e.Node.CheckState);
}
/// <summary>
/// 设置子节点的状态
/// </summary>
/// <param name="node"></param>
/// <param name="check"></param>
private void SetCheckedChildNodes(DevExpress.XtraTreeList.Nodes.TreeListNode node, CheckState check)
{
for (int i = 0; i < node.Nodes.Count; i++)
{
node.Nodes[i].CheckState = check;
SetCheckedChildNodes(node.Nodes[i], check);
}
}
/// <summary>
/// 设置父节点的状态
/// </summary>
/// <param name="node"></param>
/// <param name="check"></param>
private void SetCheckedParentNodes(DevExpress.XtraTreeList.Nodes.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);
}
}
在设计界面,treeList的属性设置上,选择BeforeCheckNode和AfterCheckNode
就可以实现父节点勾选则子节点全部勾选了