1

在我下面的代码中,我有一个 AccessId 列表,它存储在树节点的标记中。如何检查它们的标签在我的列表中的节点(或子节点)?

List<AccessFieldSet> AccessList = new List<AccessFieldSet>();
private void GetRolesAccessData(Int32 RolesId)
{            
    C_RolesUsers Db = new C_RolesUsers();
    AccessList = Db.GetRolesAccessData(RolesId);
    foreach (AccessFieldSet Afs in AccessList)
    {
        foreach (TreeNode node in TreeRoles.Nodes)
        {
            if (node.Tag == Afs.AccessId.ToString())
            {
                //Check Node is true ?
            }
            GetTagChildren(node);
        }
    }
}

private void GetTagChildren(TreeNode Node)
{
    TreeNode ChNode = null;
    //TreeFieldSet nodeCat = (TreeFieldSet)Node.Tag;
    //Int32 nodeCat = (Int32)Node.Tag;
    foreach (AccessFieldSet Afs in AccessList)
    {
        if (Afs.AccessId.ToString() == Node.Tag)
        {
            //Check Node is true ?
            GetTagChildren(ChNode);
        }
    }
}

我将代码编辑为:

List<AccessFieldSet> AccessList = new List<AccessFieldSet>();
private void GetRolesAccessData(Int32 RolesId)
{            
    C_RolesUsers Db = new C_RolesUsers();
    AccessList = Db.GetRolesAccessData(RolesId);
    for (int i = 0; i < TreeRoles.Nodes.Count; i++)
    {
        CheckedSelectedNodes(i, TreeRoles.Nodes, AccessList);
    }
}

private void CheckedSelectedNodes(Int32 i, TreeNodeCollection nodes, List<AccessFieldSet> AccessList)
{
    TreeNode node = nodes[i];
    for (int j = 0; j < AccessList.Count; j++)
    {
        foreach (AccessFieldSet Afs in AccessList)
        {
            if ((int)node.Tag == Afs.AccessId)
            {
                node.Checked = true;
            }
        }
    }
}

使用新代码,我可以检查节点标记在 AccessList 中的节点。如何更改 ChildNodes(节点的孩子)的代码?

4

1 回答 1

1

您可以使用递归来非常优雅地做到这一点。

List<AccessFieldSet> accessList = new List<AccessFieldSet>(); 

private void GetRolesAccessData(Int32 RolesId) 
{             
    C_RolesUsers Db = new C_RolesUsers(); 
    accessList = Db.GetRolesAccessData(RolesId);
    foreach (TreeNode node in TreeRoles.Nodes)
    {
        CheckNodeRecursively(node, accessList);
    } 
} 

private void CheckNodeRecursively(TreeNode node, List<AccessFieldSet> accessList) 
{
    // Note: You don't need the for loop through 'j'.
    foreach (AccessFieldSet afs in accessList) 
    { 
        if ((int)node.Tag == afs.AccessId) 
        { 
            node.Checked = true; 
        } 
    }
    foreach (TreeNode childNode in node.Nodes)
    {
        CheckNodeRecursively(childNode, accessList);
    }
} 

如果您发现自己经常做这种事情,您可以将递归分解为实用方法并使用 Action 来改变“每个节点上的操作”逻辑。例如:

public static void ActOnAllRecursively(this TreeNodeCollection nodes, Action<TreeNode> action)
{
    foreach (TreeNode node in nodes)
    {
        ActOnRecursively(node, action);
    }
}

public static void ActOnRecursively(this TreeNode node, Action<TreeNode> action)
{
    action(node);
    foreach (TreeNode node in node.Nodes)
    {
        ActOnRecursively(node);
    }
}

然后你的代码看起来像这样:

List<AccessFieldSet> accessList = new List<AccessFieldSet>(); 

private void GetRolesAccessData(Int32 RolesId) 
{             
    C_RolesUsers Db = new C_RolesUsers(); 
    accessList = Db.GetRolesAccessData(RolesId);
    TreeRoles.Nodes.ActOnAllRecursively((node) =>
    {
        foreach (AccessFieldSet afs in accessList) 
        { 
            if ((int)node.Tag == afs.AccessId) 
            { 
                node.Checked = true; 
            } 
        } 
    });
}

最后,您可以疯狂地使用 LINQ 并以声明式样式编写特定于应用程序的代码:

List<AccessFieldSet> accessList = new List<AccessFieldSet>(); 

private void GetRolesAccessData(Int32 RolesId) 
{             
    C_RolesUsers Db = new C_RolesUsers(); 
    accessList = Db.GetRolesAccessData(RolesId);
    TreeRoles.Nodes.ActOnAllRecursively((node) =>
    {
        node.Checked = accessList.Any(afs => afs.AccessId == (int)node.Tag);
    });
}
于 2012-06-06T05:29:43.943 回答