添加项目文件。
This commit is contained in:
178
Wpf.Ui.Extend/Models/TreeModel.cs
Normal file
178
Wpf.Ui.Extend/Models/TreeModel.cs
Normal file
@@ -0,0 +1,178 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
|
||||
namespace Wpf.Ui.Extend.Models;
|
||||
|
||||
|
||||
public partial class TreeModel : ObservableObject, IComparable
|
||||
{
|
||||
/// <summary>
|
||||
/// 构造
|
||||
/// </summary>
|
||||
public TreeModel()
|
||||
{
|
||||
Children = new ObservableCollection<TreeModel>();
|
||||
isChecked = false;
|
||||
Icon = "./Resources/File_16px.png";
|
||||
//TitleIcon = 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
|
||||
{
|
||||
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; }
|
||||
|
||||
/// <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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user