示例:WPF中TreeView自定义TreeNode泛型绑定对象来实现级联勾选

一、目的:在绑定TreeView的功能中经常会遇到需要在树节点前增加勾选CheckBox框,勾选本节点的同时也要同步显示父节点和子节点状态

二、实现

三、环境

VS2022

四、示例

定义如下节点类

复制代码
   public partial class TreeNodeBase<T> : SelectBindable<T>, ITreeNode
    {
        public TreeNodeBase(T t) : base(t)
        {

        }


        private bool? _isChecked = false;
        public bool? IsChecked
        {
            get { return _isChecked; }
            set
            {
                _isChecked = value;
                RaisePropertyChanged();
                RefreshParentCheckState();
                RefreshChildrenCheckState();
            }
        }


        private void RefreshParentCheckState()
        {
            if (Parent == null)
                return;

            bool allChecked = Parent.Nodes.All(l => l.IsChecked == true);
            if (allChecked)
            {
                Parent.CheckOnlyCurrent(true);
                Parent.RefreshParentCheckState();
                return;
            }

            bool allUnChecked = Parent.Nodes.All(l => l.IsChecked == false);
            if (allUnChecked)
            {
                Parent.CheckOnlyCurrent(false);
                Parent.RefreshParentCheckState();
                return;
            }

            Parent.CheckOnlyCurrent(null);
            Parent.RefreshParentCheckState();

        }

        private void RefreshChildrenCheckState()
        {
            foreach (TreeNodeBase<T> item in Nodes)
            {
                item.CheckOnlyCurrent(IsChecked);
                item.RefreshChildrenCheckState();
            }
        }

        private void CheckOnlyCurrent(bool? value)
        {
            _isChecked = value;
            RaisePropertyChanged("IsChecked");
        }

        public TreeNodeBase<T> Parent { get; set; }

        private ObservableCollection<TreeNodeBase<T>> _nodes = new ObservableCollection<TreeNodeBase<T>>();

        public ObservableCollection<TreeNodeBase<T>> Nodes
        {
            get { return _nodes; }
            set
            {
                _nodes = value;
                RaisePropertyChanged();
            }
        }

        public void AddNode(TreeNodeBase<T> node)
        {
            node.Parent = this;
            Nodes.Add(node);
        }
    }

其中核心方法是如下方法

分别在当前节点勾选有变化时去更新父节点和子节点的勾选状态

五、需要了解的知识点

TreeView 类 (System.Windows.Controls) | Microsoft Learn

六、源码地址

GitHub - HeBianGu/WPF-ControlDemo: 示例

GitHub - HeBianGu/WPF-ControlBase: Wpf封装的自定义控件资源库

GitHub - HeBianGu/WPF-Control: WPF轻量控件和皮肤库

七、了解更多

System.Windows.Controls 命名空间 | Microsoft Learn

https://github.com/HeBianGu

HeBianGu的个人空间-HeBianGu个人主页-哔哩哔哩视频

相关推荐
分布式存储与RustFS6 小时前
告别手动配置:用 Terraform 定义你的 RustFS 存储帝国
云原生·wpf·文件系统·terraform·对象存储·minio·rustfs
c#上位机21 小时前
wpf之TabControl
c#·wpf
mingupup21 小时前
WPF应用最小化到系统托盘
wpf
qiangshang9901261 天前
WPF+MVVM入门学习
学习·wpf
DASXSDW1 天前
Abp vNext-事件总线使用实现及解析
ui·wpf
纸照片1 天前
【邪修玩法】如何在WPF中开放 RESTful API 服务
后端·wpf·restful
啊丢_2 天前
WPF基本布局容器与控件
wpf
c#上位机2 天前
wpf之RelativeSource用法总结
c#·wpf
玖笙&5 天前
✨WPF编程基础【2.1】布局原则
c++·wpf·visual studio
玖笙&5 天前
✨WPF编程基础【2.2】:布局面板实战
c++·wpf·visual studio