using HxModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HxUserManagement.Classes
{
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; }
///
/// 项目ID
///
public string ProjectId { get; set; }
///
/// 子节点ID
///
public string ChildrenId { get; set; }
///
/// 状态(0禁用,1启用)
///
public int State { get; set; }
///
/// 层级
///
public int Level { get; set; }
///
/// 菜单名称
///
public string Name { get; set; }
///
/// 默认图片
///
public string ImgDefault { get; set; }
///
/// 点击后图片
///
public string ImgChange { get; set; }
///
/// 排序
///
public int Sequence { get; set; }
///
/// 创建人
///
public string CreatName { get; set; }
///
/// 创建时间
///
public DateTime CreateTime { get; set; }
///
/// 修改人
///
public string ModifyName { get; set; }
///
/// 修改时间
///
public DateTime? ModifyTime { get; set; }
public MenuTree Parent { get; set; }
public List Children { get; set; }
public bool? _isChecked;
///
/// CheckBox是否选中
///
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();
}
public MenuTree(MenuModel menu)
{
this.Id = menu.Id;
this.ProjectId = menu.ProjectId;
this.ChildrenId = menu.ChildrenId;
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 = menu.CreateTime;
this.ModifyName = menu.ModifyName;
this.ModifyTime = menu.ModifyTime;
this.IsExpanded = true;
//_isChecked = null;
this.Children = new List();
}
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");
}
///
/// 检查父类是否选 中
/// 如果父类的子类中有一个和第一个子类的状态不一样父类ischecked为null
///
private void CheckParentCheckState()
{
List checkedItems = new List();
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);
}
///
/// 创建树
///
///
///
public void CreateTreeWithChildren(MenuTree children, bool? isChecked)
{
this.Children.Add(children);
//必须先把孩子加入再为Parent赋值,
//否则当只有一个子节点时Parent的IsChecked状态会出错
//if(children.Name == )
children.Parent = this;
children.IsChecked = isChecked;
}
}
}