Dev控件 TreeList的使用,绑定多级树以及多选框的三种状态

TreeList控件可以同时显示树结构和其他数据列,即在一个列上建立父子关系展开或收缩,同时还可以显示其他列的内容。

在TreeList中同时引入了
Node和 Columns概念,
第一列为树结构,
其余列为数据列,
任何列都可作为树结构列(即拖动到第一列)显示

1.实现多级树---数据源绑定

在TreeList里添加要显示列(第一列为树结构,其他列为数据列),并指定FieldName为数据库列名

指定KeyFieldName属性为主键,ParentFieldName属性为树状分组列。注意
ParentFieldName指定的列如果为空值,树状可能会混乱(只有一个根结点,其他为此节点的子结点)

用TreeList.DataSource = DataTable 绑定数据即可

(以下内容为转载)多选框的三种状态

树形控件是使用频率很高的一种控件。对于属性控件往往需要下面两个功能
1.TreeList带有CheckBox,并且节点要有三种状态(所有的子节点都选中,所有的子节点都没选择,一部分子节点选中)。使用DevXpress的TreeList控件很容易实现这一功能。
设置TreeList.OptionsView.ShowCheckBoxes = true //是否显示CheckBox
设置TreeList.OptionsBehavior.AllowIndeterminateCheckState = true; //设置节点是否有中间状态,即一部分子节点选中,一部分子节点没有选中
设置这两个属性之后就实现了TreeList带有CheckBox,并且节点有三种状态。
2.选中父节点或者子节点相互影响的功能,如选择父节点选择所有子节点。绑定TreeList的两个事件AfterCheckNode和BeforeCheckNode
实现功能的代码如下:
private void treeList1_AfterCheckNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e)
{
SetCheckedChildNodes(e.Node, e.Node.CheckState);
SetCheckedParentNodes(e.Node, e.Node.CheckState);
}
private void treeList1_BeforeCheckNode(object sender, DevExpress.XtraTreeList.CheckNodeEventArgs e)
{
e.State = (e.PrevState == CheckState.Checked ? CheckState.Unchecked : CheckState.Checked);
}
///
/// 设置子节点的状态
///
///
///
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);
}
}
///
/// 设置父节点的状态
///
///
///
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);
}
}
VB.net语言代码如下
Private Sub trlContact_AfterCheckNode(ByVal sender As System.Object, ByVal e As DevExpress.XtraTreeList.NodeEventArgs) Handles trlContact.AfterCheckNode
SetCheckedChildNodes(e.Node, e.Node.CheckState)
SetCheckedParentNodes(e.Node, e.Node.CheckState)
End Sub
‘设置子结点的状态
Private Sub SetCheckedChildNodes(ByVal node As TreeListNode, ByVal check As CheckState)

    For i As Integer = 0 To node.Nodes.Count - 1
        node.Nodes(i).CheckState = check
        SetCheckedChildNodes(node.Nodes(i), check)
    Next
End Sub
'设置父结点的状态
Private Sub SetCheckedParentNodes(ByVal node As TreeListNode, ByVal check As CheckState)
    If node.ParentNode Is Nothing = False Then
        Dim b As Boolean = False
        Dim state As CheckState
        For i As Integer = 0 To node.ParentNode.Nodes.Count - 1
            state = node.ParentNode.Nodes(i).CheckState
            If check.Equals(state) = False Then
                b = Not b
                Exit For
            End If
        Next

        If b Then
            node.ParentNode.CheckState = CheckState.Indeterminate
        Else
            node.ParentNode.CheckState = check
        End If

        SetCheckedParentNodes(node.ParentNode, check)
    End If
End Sub


Private Sub trlContact_BeforeCheckNode(ByVal sender As System.Object, ByVal e As DevExpress.XtraTreeList.CheckNodeEventArgs) Handles trlContact.BeforeCheckNode

    If e.PrevState = CheckState.Checked Then
        e.State = CheckState.Unchecked
    Else
        e.State = CheckState.Checked
    End If

End Sub!

[效果如下]
(https://img-blog.csdn.net/20150717165032622)

转载自 Dev控件 TreeList的使用,绑定多级树以及多选框的三种状态 | 学步园 +http://www.xuebuyuan.com/1422861.html

你可能感兴趣的:(编程语言)