schangxiang@126.com
2025-11-04 f5ed29dc26c7cd952d56ec5721a2efc43cd25992
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
using HxModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DataEntity.User;
namespace XHandler.Class
{
    public class MenuTree:INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propname)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propname));
            }
        }
 
        public string Id { get; set; }
 
        /// <summary>
        /// 项目ID
        /// </summary>
        public string ProjectId { get; set; }
 
        /// <summary>
        /// 子节点ID
        /// </summary>
        public string ChildrenId { get; set; }
 
        /// <summary>
        /// 状态(0禁用,1启用)
        /// </summary> 
        public int State { get; set; }
 
        /// <summary>
        /// 层级
        /// </summary>
        public int Level { get; set; }
 
        /// <summary>
        /// 菜单名称
        /// </summary>
        public string Name { get; set; }
 
        /// <summary>
        /// 默认图片
        /// </summary>
        public string ImgDefault { get; set; }
 
        /// <summary>
        /// 点击后图片
        /// </summary>
        public string ImgChange { get; set; }
 
        /// <summary>
        /// 排序
        /// </summary>
        public int Sequence { get; set; }
 
        /// <summary>
        /// 创建人
        /// </summary>
        public string CreatName { get; set; }
 
        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime { get; set; }
 
        /// <summary>
        /// 修改人
        /// </summary>
        public string ModifyName { get; set; }
 
        /// <summary>
        /// 修改时间
        /// </summary>
        public DateTime? ModifyTime { get; set; }
        public MenuTree Parent { get; set; }
        public List<MenuTree> Children { get; set; }
 
        public bool? _isChecked;
        /// <summary>
        /// CheckBox是否选中
        /// </summary>
        public bool? IsChecked
        {
            get
            {
                return _isChecked;
            }
            set
            {
                SetIsChecked(value, true, true);
            }
        }
 
        private bool _isExpanded;
        public bool IsExpanded
        {
            get
            {
                return _isExpanded;
            }
            set
            {
                _isExpanded = value;
                OnPropertyChanged("IsExpanded");
            }
        }
 
        public MenuTree()
        {
            Level = -1;
            IsExpanded = true;
            //_isChecked = false;
            this.Children = new List<MenuTree>();
        }
        public MenuTree(MenuTab menu)
        {
            this.Id = menu.menu_id;
            this.ProjectId = menu.project_id;
            this.ChildrenId = menu.children_id;
            this.State = menu.state;
            this.Level = menu.level;
            this.Name = menu.name;
            this.ImgDefault = menu.imgdefault;
            this.ImgChange = menu.imgchange;
            this.Sequence = menu.sequence;
            this.CreatName = menu.creatname;
            this.CreateTime = Convert.ToDateTime(menu.createtime==null? "2022-10-09 00:01:10" : menu.createtime);
            this.ModifyName = menu.modifyname;
            this.ModifyTime = Convert.ToDateTime((menu.modifytime==null|| menu.modifytime =="") ? "2022-10-09 00:01:10" : menu.modifytime);
            this.IsExpanded = true;
            //_isChecked = null;
            this.Children = new List<MenuTree>();
        }
 
        private void SetIsChecked(bool? value, bool checkedChildren, bool checkedParent)
        {
            if (_isChecked == value) return;
            _isChecked = value;
            //选中和取消子类
            if (checkedChildren && value.HasValue && Children != null)
                Children.ForEach(ch => ch.SetIsChecked(value, true, false));
 
            //选中和取消父类
            if (checkedParent && this.Parent != null)
                this.Parent.CheckParentCheckState();
 
            //通知更改
            OnPropertyChanged("IsChecked");
        }
 
        /// <summary>
        /// 检查父类是否选 中
        /// 如果父类的子类中有一个和第一个子类的状态不一样父类ischecked为null
        /// </summary>
        private void CheckParentCheckState()
        {
            List<MenuTree> checkedItems = new List<MenuTree>();
            string checkedNames = string.Empty;
            bool? _currentState = this.IsChecked;
            bool? _firstState = null;
            for (int i = 0; i < this.Children.Count(); i++)
            {
                bool? childrenState = this.Children[i].IsChecked;
                if (i == 0)
                {
                    _firstState = childrenState;
                }
                else if (_firstState != childrenState)
                {
                    _firstState = null;
                }
            }
            if (_firstState != null) _currentState = _firstState;
            SetIsChecked(_firstState, false, true);
        }
 
        /// <summary>
        /// 创建树
        /// </summary>
        /// <param name="children"></param>
        /// <param name="isChecked"></param>
 
        public void CreateTreeWithChildren(MenuTree children, bool? isChecked)
        {
            this.Children.Add(children);
            //必须先把孩子加入再为Parent赋值,
            //否则当只有一个子节点时Parent的IsChecked状态会出错
            //if(children.Name == )
            children.Parent = this;
            children.IsChecked = isChecked;
        }
    }
}