Files
ShrlAlgoToolkit/Wpf.Ui.Extend/Models/TreeModel.cs

203 lines
4.9 KiB
C#
Raw Normal View History

2024-09-22 11:05:41 +08:00
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
2024-10-08 16:21:39 +08:00
using System.ComponentModel;
2024-09-22 11:05:41 +08:00
using System.Linq;
2024-10-08 16:21:39 +08:00
using System.Runtime.CompilerServices;
2024-09-22 11:05:41 +08:00
namespace Wpf.Ui.Extend.Models;
2024-10-08 16:21:39 +08:00
public partial class TreeModel : INotifyPropertyChanged, IComparable
2024-09-22 11:05:41 +08:00
{
/// <summary>
/// 构造
/// </summary>
public TreeModel()
{
Children = new ObservableCollection<TreeModel>();
2024-10-08 16:21:39 +08:00
IsChecked = false;
//Icon = "./Resources/File_16px.png";
2024-09-22 11:05:41 +08:00
//TitleIcon = FamilyPlugins.Properties.Resources.Folder_32px;
IsExpanded = false;
}
2024-10-08 16:21:39 +08:00
public event PropertyChangedEventHandler PropertyChanged;
protected void SetProperty<T>(ref T prop, T value, [CallerMemberName] string propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(prop, value) == false)
{
prop = value;
OnPropertyChanged(propertyName);
}
}
2024-09-22 11:05:41 +08:00
2024-10-08 16:21:39 +08:00
public virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
2024-09-22 11:05:41 +08:00
private bool? isChecked;
2024-10-08 16:21:39 +08:00
2024-09-22 11:05:41 +08:00
private bool isExpanded;
/// <summary>
/// 指针悬停时的显示说明
/// </summary>
public string ToolTip
{
get
{
return Parent == null ? $"{Name}" : $"{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; }
2024-10-08 16:21:39 +08:00
/// <summary>
/// 选中状态
/// </summary>
public bool? IsChecked
{
get => isChecked;
set => SetProperty(ref isChecked, value);
}
/// <summary>
/// 折叠状态
/// </summary>
public bool IsExpanded
{
get => isExpanded;
set => SetProperty(ref isExpanded, value);
}
2024-09-22 11:05:41 +08:00
/// <summary>
/// 排序
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public int CompareTo(object obj)
{
var treeModel = (TreeModel)obj;
int result;
if (Name == treeModel.Name)
{
result = 0;
}
else
{
result = Name.CompareTo(treeModel.Name) > 0 ? 1 : -1;
}
return result;
}
/// <summary>
/// 设置所有子项的选中状态
/// </summary>
public void SetChildrenChecked()
{
if (Children != null && Children.Count > 0 && IsChecked != null)
{
foreach (var 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 (var 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 (var 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();
}
}
}
}