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; }
|
|
/// <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(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<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;
|
}
|
}
|
}
|