Files
RevitArchive/TreeModel.cs
2026-02-23 14:58:05 +08:00

200 lines
5.2 KiB
C#

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
{
/// <summary>
/// 构造
/// </summary>
public TreeModel()
{
Children = new ObservableCollection<TreeModel>();
isChecked = false;
Icon = "./Resources/File_16px.png";
//Icon = FamilyPlugins.Properties.Resources.Folder_32px;
IsExpanded = false;
}
/// <summary>
/// 选中状态
/// </summary>
[ObservableProperty]
private bool? isChecked;
/// <summary>
/// 折叠状态
/// </summary>
[ObservableProperty]
private bool isExpanded;
/// <summary>
/// 指针悬停时的显示说明
/// </summary>
public string ToolTip
{
get
{
if (Parent == null)
{
return string.Format(Name);
}
return $"{Parent.Name}-{Name}";
//return String.Format(Name);
}
}
/// <summary>
/// 子项
/// </summary>
public IList<TreeModel> Children { get; set; }
public object Tag { get; set; }
/// <summary>
/// 键值
/// </summary>
public string Id { get; set; }
/// <summary>
/// 显示的字符
/// </summary>
public string Name { get; set; }
/// <summary>
/// 图标
/// </summary>
public string Icon { get; set; }
/// <summary>
/// 父项
/// </summary>
public TreeModel Parent { get; set; }
/// <summary>
/// 排序
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 设置所有子项的选中状态
/// </summary>
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();
}
}
}
}
/// <summary>
/// 设置所有子项的选中状态
/// </summary>
/// <param name="isCheck"></param>
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);
}
}
}
}
/// <summary>
/// 设置所有子项展开状态
/// </summary>
/// <param name="isExpanded"></param>
public void SetChildrenExpanded(bool isExpanded)
{
foreach (TreeModel child in Children)
{
child.IsExpanded = isExpanded;
child.SetChildrenExpanded(isExpanded);
}
}
/// <summary>
/// 使父节点处于选中状态
/// </summary>
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();
}
}
}
}
}