首页 > 代码库 > [DevExpress]禁用TreeListNode CheckBox

[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<style type="text/css">.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }</style>

希望有所帮助!