WPF 在TreeView使用CheckBox实现联动——全选和反选

 

XAML前端代码:


    
        
            
                
                    
                    
                    
                
            
        
    

TreeCategory【实现INotifyPropertyChanged接口】:

public class TreeCategory : INotifyPropertyChanged
    {
        private int id;
        private string title;
        private bool isCheck;
        private int parentID;
        private ObservableCollection children;
        private string icon;
        /// 
        /// 节点编号
        /// 
        public int Id { get { return id; } set { id = value; OnPropertyChanged("Id"); } }
        /// 
        /// 名称
        /// 
        public string Title { get { return title; } set { title = value; OnPropertyChanged("Title"); } }
        /// 
        /// 是否选中
        /// 
        public bool IsCheck { get { return isCheck; } set { isCheck = value; OnPropertyChanged("IsCheck"); } }
        /// 
        ///父节点
        /// 
        public int ParentID { get { return parentID; } set { parentID = value; OnPropertyChanged("ParentID"); } }
        /// 
        /// 子节点
        /// 
        public ObservableCollection Children
        {
            get
            {
                if (children == null)
                {
                    children = new ObservableCollection();
                }
                return children;
            }
            set
            {
                if (children == null)
                {
                    children = new ObservableCollection();
                }
                children = value; OnPropertyChanged("Children");
            }
        }
        /// 
        /// 图标
        /// 
        public string Icon { get { return icon; } set { icon = value; OnPropertyChanged("Icon"); } }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

 

后台代码:

/// 
    /// MainWindow.xaml 的交互逻辑
    /// 
    public partial class MainWindow : Window
    {
        private ObservableCollection obList=new ObservableCollection ();
        public MainWindow()
        {
            InitializeComponent();
            this.treeView.ItemsSource = InitData();
        }
        /// 
        /// 数据初始化
        /// 
        /// 
        private ObservableCollection InitData()
        {
            ObservableCollection tcList = new ObservableCollection() {
                new TreeCategory(){Id = 1,Title = "Node1.",ParentID = 0},
                new TreeCategory(){Id = 2,Title = "Node2.",ParentID = 0},
                new TreeCategory(){Id = 3,Title = "Node3.",ParentID = 1},
                new TreeCategory(){Id = 4,Title = "Node4.",ParentID = 3},
                new TreeCategory(){Id = 5,Title = "Node5.",ParentID = 3},
                new TreeCategory(){Id = 6,Title = "Node6.",ParentID = 4},
                new TreeCategory(){Id = 7,Title = "Node7.",ParentID = 2},
                new TreeCategory(){Id = 8,Title = "Node8.",ParentID = 2},
                new TreeCategory(){Id = 9,Title = "Node9.",ParentID = 8},
            };
            //最开始的父节点默认为0,如果不递归,TreeView并不会生成【树】
            obList = FindChild(tcList, 0);
            return obList;
        }
        /// 
        /// 遍历子类
        /// 
        /// 
        /// 父类编号
        /// 
        private ObservableCollection FindChild(ObservableCollection tcList,int id)
        {
            ObservableCollection childList = new ObservableCollection();
            foreach (var item in tcList)
            {
                if (item.ParentID == id)
                {
                    childList.Add(item);
                    //判断是否有子节点
                    int count = tcList.Where(a => a.ParentID == item.Id).Count();
                    if (count > 0)
                    {
                        item.Children = FindChild(tcList, item.Id);
                    }
                }
            }
            return childList;
        }

        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            CheckBox checkBox = (CheckBox)sender;
            int id =Convert.ToInt32(checkBox.Tag);
            //寻找被点击的checkbox
            TreeCategory trees = FindNode(this.treeView.Items.Cast().ToList(), id);
            //如果有子类,则子类状态和被点击的CheckBox状态一致
            if (trees.Children.Count()>0)
            {
                IsCheckBoxed(trees.Children, Convert.ToBoolean(checkBox.IsChecked));
            }
        }
        /// 
        /// 寻找被点击的CheckBox
        /// 
        /// 
        /// 需要寻找的ID
        /// 
        private TreeCategory FindNode(List Nodes, int id)
        {
            TreeCategory tree = new TreeCategory();
            foreach (var item in Nodes)
            {
                if (item.Id!= id)
                {
                    tree= FindNode(item.Children.ToList(), id);                   
                }
                else
                {
                    tree = item;
                }
                if (tree.Id==id)
                {
                    return tree;
                }
            }
            return tree;
        }
        /// 
        /// 更改子节点状态
        /// 
        /// 子节点集合
        /// true?flag
        private void IsCheckBoxed(ObservableCollection tcList,bool flag)
        {
            foreach (var item in tcList)
            {
                item.IsCheck = flag;
                if (item.Children.Count() > 0)
                {
                    IsCheckBoxed(item.Children, flag);
                }
            }
        }
    }

要想子父节点联动,需要使用递归。 


效果图:

WPF 在TreeView使用CheckBox实现联动——全选和反选_第1张图片

 

 

你可能感兴趣的:(checkbox,wpf,C#,TreeView,#,WPF)