using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using CommunityToolkit.Mvvm.ComponentModel;
namespace SZBIM.StandardTools
{
public partial class TreeModel : ObservableObject, IComparable
{
///
/// 构造
///
public TreeModel()
{
Children = new ObservableCollection();
isChecked = false;
Icon = "./Resources/File_16px.png";
//Icon = FamilyPlugins.Properties.Resources.Folder_32px;
IsExpanded = false;
}
///
/// 选中状态
///
[ObservableProperty]
private bool? isChecked;
///
/// 折叠状态
///
[ObservableProperty]
private bool isExpanded;
///
/// 指针悬停时的显示说明
///
public string ToolTip
{
get
{
if (Parent == null)
{
return string.Format(Name);
}
return $"{Parent.Name}-{Name}";
//return String.Format(Name);
}
}
///
/// 子项
///
public IList Children { get; set; }
public object Tag { get; set; }
///
/// 键值
///
public string Id { get; set; }
///
/// 显示的字符
///
public string Name { get; set; }
///
/// 图标
///
public string Icon { get; set; }
///
/// 父项
///
public TreeModel Parent { get; set; }
///
/// 排序
///
///
///
public int CompareTo(object obj)
{
TreeModel treeModel = (TreeModel)obj;
int result;
if (Name == treeModel.Name)
{
result = 0;
}
else
{
if (Name.CompareTo(treeModel.Name) > 0)
{
result = 1;
}
//else if (this.Name == treeModel.Name)
//{
// result = 1;
//}
else
{
result = -1;
}
}
return result;
}
///
/// 设置所有子项的选中状态
///
public void SetChildrenChecked()
{
if (Children != null && Children.Count > 0 && IsChecked != null)
{
foreach (TreeModel child in Children)
{
child.IsChecked = IsChecked;
if (child.Children != null && Children.Count > 0)
{
child.SetChildrenChecked();
}
}
}
}
///
/// 设置所有子项的选中状态
///
///
public void SetChildrenChecked(bool isCheck)
{
if (Children != null && Children.Count > 0)
{
foreach (TreeModel child in Children)
{
child.IsChecked = isCheck;
if (child.Children != null && Children.Count > 0)
{
child.SetChildrenChecked(isCheck);
}
}
}
}
///
/// 设置所有子项展开状态
///
///
public void SetChildrenExpanded(bool isExpanded)
{
foreach (TreeModel child in Children)
{
child.IsExpanded = isExpanded;
child.SetChildrenExpanded(isExpanded);
}
}
///
/// 使父节点处于选中状态
///
public void SetParentChecked()
{
if (Parent != null)
{
if (Parent.Children.Count(c => c.IsChecked == null) > 0)
{
Parent.IsChecked = null;
}
else if (Parent.Children.Count(c => c.IsChecked == true) == 0)
{
Parent.IsChecked = false;
}
else if (Parent.Children.Count(c => c.IsChecked == true) == Parent.Children.Count)
{
Parent.IsChecked = true;
}
else if (Parent.Children.Count(c => c.IsChecked == true) < Parent.Children.Count)
{
Parent.IsChecked = null;
}
if (Parent.Parent != null)
{
Parent.SetParentChecked();
}
}
}
}
}