using System; using System.Collections.ObjectModel; using System.Linq; using System.Runtime.CompilerServices; namespace WPFluent.Models; public partial class TreeModel : INotifyPropertyChanged, IComparable { private bool? isChecked; private bool isExpanded; /// /// 构造 /// public TreeModel() { Children = new ObservableCollection(); IsChecked = false; //Icon = "./Resources/File_16px.png"; //TitleIcon = FamilyPlugins.Properties.Resources.Folder_32px; IsExpanded = false; } public event PropertyChangedEventHandler? PropertyChanged; protected void SetProperty(ref T prop, T value, [CallerMemberName] string propertyName = null) { if(EqualityComparer.Default.Equals(prop, value) == false) { prop = value; OnPropertyChanged(propertyName); } } /// /// 排序 /// /// /// 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; } public virtual void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } /// /// 设置所有子项的选中状态 /// 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(); } } } } /// /// 设置所有子项的选中状态 /// /// 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); } } } } /// /// 设置所有子项展开状态 /// /// public void SetChildrenExpanded(bool isExpanded) { foreach(var 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(); } } } /// /// 子项 /// public IList Children { get; set; } /// /// 图标 /// public string Icon { get; set; } /// /// 键值 /// public string Id { get; set; } /// /// 选中状态 /// public bool? IsChecked { get => isChecked; set => SetProperty(ref isChecked, value); } /// /// 折叠状态 /// public bool IsExpanded { get => isExpanded; set => SetProperty(ref isExpanded, value); } /// /// 显示的字符 /// public string Name { get; set; } /// /// 父项 /// public TreeModel Parent { get; set; } public object Tag { get; set; } /// /// 指针悬停时的显示说明 /// public string ToolTip { get { return Parent == null ? $"{Name}" : $"{Parent.Name}-{Name}"; //return String.Format(Name); } } }