[DevExpress]禁用TreeListNode CheckBox

在实际开发中,需要根据条件来设置节点不可勾选,查看DevExpress文档发现通过其CustomDrawNodeCheckBox和BeforeCheckNode事件组合使用可以达到,一般设置节点不可以勾选的规则是一样的,所以对代码进行稍微封装。

关键代码:

        /// <summary>

        /// 禁用CheckBox

        /// 说明

        /// 在CustomDrawNodeCheckBox事件中使用

        /// </summary>

        /// <param name="tree">TreeList</param>

        /// <param name="conditionHanlder">委托</param>

        /// <param name="e">CustomDrawNodeCheckBoxEventArgs</param>

        public static void DisabledCheckBox(this TreeListNode tree, Predicate<TreeListNode> conditionHanlder, CustomDrawNodeCheckBoxEventArgs e)

        {

            if (conditionHanlder(e.Node))

            {

                e.ObjectArgs.State = ObjectState.Disabled;

            }

        }

        /// <summary>

        /// 禁止操作节点CheckBox

        /// 说明

        /// 在BeforeCheckNode事件中使用

        /// </summary>

        /// <param name="tree">TreeListNode</param>

        /// <param name="conditionHanlder">委托</param>

        /// <param name="e">CheckNodeEventArgs</param>

        public static void DisabledSetCheckBox(this TreeListNode tree, Predicate<TreeListNode> conditionHanlder, CheckNodeEventArgs e)

        {

            if (conditionHanlder(e.Node))

            {

                e.CanCheck = false;

            }

        }

使用示例:

        /// <summary>

        /// CustomDrawNodeCheckBox事件

        /// </summary>

        void LHTree_CustomDrawNodeCheckBox(object sender, CustomDrawNodeCheckBoxEventArgs e)

        {

            e.Node.DisabledCheckBox(DisableNodeCheckBoxRule, e);

        }

        /// <summary>

        /// BeforeCheckNode事件

        /// </summary>

        void LHTree_BeforeCheckNode(object sender, CheckNodeEventArgs e)

        {

            e.Node.DisabledSetCheckBox(DisableNodeCheckBoxRule, e);

        }

        /// <summary>

        /// Disable TreeListNode规则

        /// </summary>

        /// <param name="node"></param>

        /// <returns></returns>

        private bool DisableNodeCheckBoxRule(TreeListNode node)

        {

            bool _disable = false;

            if (OpLampCfgList.Count > 0)

            {

                string _cabId = OpLampCfgList[0].CabID;

                NodeType _curType = node.GetNodeType();

                if (_curType == NodeType.Lamp)//当节点是灯类型且父节点KEYID等于_cabId时候,屏蔽Checkbox

                {

                    _disable = node.ParentNode.ParentNode.GetKeyID().Equals(_cabId);

                }

                if (_curType == NodeType.Pole)//当节点是杆类型且父节点KEYID等于_cabId时候,屏蔽Checkbox

                {

                    _disable = node.ParentNode.GetKeyID().Equals(_cabId);

                }

                if (_curType == NodeType.Cab && node.GetKeyID().Equals(_cabId))//当节点是箱类型KEYID等于_cabId时候,屏蔽Checkbox

                {

                    _disable = true;

                }

                if (_curType == NodeType.Area || _curType == NodeType.CabsGroupRoot)//当节点类型等于地域或者根节点时候,屏蔽Checkbox

                {

                    _disable = true;

                }

            }

            return _disable;

        }

代码效果:

image

希望有所帮助!

你可能感兴趣的:(DevExpress)