Files

232 lines
6.2 KiB
C#
Raw Permalink Normal View History

2025-09-16 16:06:41 +08:00
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
namespace Szmedi.RvKits.Models
{
public class TreeModel : INotifyPropertyChanged, IComparable
{
/// <summary>
/// 是否展开
/// </summary>
public bool IsExpanded
{
get => _isExpanded;
set
{
if (value != _isExpanded)
{
//折叠状态改变
_isExpanded = value;
NotifyPropertyChanged("IsExpanded");
}
}
}
/// <summary>
/// 是否选中
/// </summary>
public bool? IsChecked
{
get => _isChecked;
set
{
if (value != _isChecked)
{
_isChecked = value;
NotifyPropertyChanged("IsChecked");
}
}
}
/// <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 string ToolTip => Parent == null ? string.Format(Name) : string.Format("{0}-{1}", Parent.Name, Name);//return String.Format(Name);
/// <summary>
/// 父项
/// </summary>
public TreeModel Parent { get; set; }
/// <summary>
/// 构造
/// </summary>
public TreeModel()
{
Children = new ObservableCollection<TreeModel>();
_isChecked = false;
//Icon = "./Images/16_16/File_16px.png";
//Icon = FamilyPlugins.Properties.Resource.Folder_32px;
IsExpanded = false;
}
#region
/// <summary>
/// 排序
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public int CompareTo(object obj)
{
var treeModel = (TreeModel)obj;
return Name == treeModel.Name ? 0 : Name.CompareTo(treeModel.Name) > 0 ? 1 : -1;
}
#endregion
#region
/// <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();
}
}
}
}
#endregion
#region
/// <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);
}
}
}
}
#endregion
#region
/// <summary>
/// 设置所有子项展开状态
/// </summary>
/// <param name="isExpanded"></param>
public void SetChildrenExpanded(bool isExpanded)
{
foreach (var child in Children)
{
child.IsExpanded = isExpanded;
child.SetChildrenExpanded(isExpanded);
}
}
#endregion
#region 使
/// <summary>
/// 使父节点处于选中状态
/// </summary>
public void SetParentChecked()
{
if (Parent != null)
{
if (Parent.Children.Where(c => c.IsChecked == null).Count() > 0)
{
Parent.IsChecked = null;
}
else if (Parent.Children.Where(c => c.IsChecked == true).Count() == 0)
{
Parent.IsChecked = false;
}
else if (Parent.Children.Where(c => c.IsChecked == true).Count() == Parent.Children.Count)
{
Parent.IsChecked = true;
}
else if (Parent.Children.Where(c => c.IsChecked == true).Count() < Parent.Children.Count)
{
Parent.IsChecked = null;
}
if (Parent.Parent != null)
{
Parent.SetParentChecked();
}
}
}
#endregion 使
#region
/// <summary>
/// 选中状态
/// </summary>
private bool? _isChecked;
/// <summary>
/// 折叠状态
/// </summary>
private bool _isExpanded;
#endregion
#region
/// <summary>
/// 属性改变事件
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
#endregion
}
}