2024-09-04 16:40:39 +04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
|
|
|
|
namespace WinFormsLibraryVolkov
|
|
|
|
|
{
|
|
|
|
|
public partial class CustomTreeCell : UserControl
|
|
|
|
|
{
|
|
|
|
|
public CustomTreeCell()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Clear()
|
|
|
|
|
{
|
|
|
|
|
treeView.Nodes.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Публичное свойство для установки и получения индекса выбранной ветки(set, get).
|
|
|
|
|
protected DataTreeNodeConfig Levels { get; set; }
|
|
|
|
|
|
|
|
|
|
public void LoadConfig(DataTreeNodeConfig levels)
|
|
|
|
|
{
|
|
|
|
|
if (levels != null)
|
|
|
|
|
{
|
|
|
|
|
Levels = levels;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-19 23:40:13 +04:00
|
|
|
|
// Метод для добавления элемента в дерево
|
2024-09-04 20:00:33 +04:00
|
|
|
|
public void AddCell<T>(int columnIndex, T element)
|
|
|
|
|
{
|
2024-09-19 23:40:13 +04:00
|
|
|
|
if (Levels == null) throw new ArgumentException("Levels равен null");
|
|
|
|
|
if (element == null) throw new ArgumentException("element равен null");
|
|
|
|
|
if (columnIndex < 0) throw new ArgumentException("индекс колонки < 0");
|
|
|
|
|
if (columnIndex >= Levels.NodeNames.Count) throw new ArgumentException("Индекс колонки > чем общее кол-во");
|
2024-09-04 20:00:33 +04:00
|
|
|
|
|
2024-09-19 23:40:13 +04:00
|
|
|
|
TreeNodeCollection treeNodeCollection = treeView.Nodes;
|
2024-09-04 20:00:33 +04:00
|
|
|
|
int num = 0;
|
|
|
|
|
foreach (string nodeName in Levels.NodeNames)
|
|
|
|
|
{
|
|
|
|
|
// Получение значения свойства или поля
|
|
|
|
|
PropertyInfo property = element.GetType().GetProperty(nodeName);
|
|
|
|
|
string text = property?.GetValue(element, null)?.ToString() ?? nodeName;
|
|
|
|
|
|
|
|
|
|
TreeNode treeNode = null;
|
|
|
|
|
foreach (TreeNode item in treeNodeCollection)
|
|
|
|
|
{
|
|
|
|
|
if (item.Text == text)
|
|
|
|
|
{
|
|
|
|
|
treeNode = item;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Если узел не найден, добавляем новый
|
|
|
|
|
treeNodeCollection = (treeNode == null) ? treeNodeCollection.Add(text).Nodes : treeNode.Nodes;
|
|
|
|
|
|
|
|
|
|
if (num >= columnIndex)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
num++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-19 23:40:13 +04:00
|
|
|
|
public T GetSelectedObject<T>() where T : class, new()
|
|
|
|
|
{
|
|
|
|
|
if (treeView.SelectedNode == null) throw new ArgumentException("treeView.SelectedNode == null");
|
|
|
|
|
if (Levels == null) throw new ArgumentException("Levels равен null");
|
|
|
|
|
if (treeView.SelectedNode.Nodes.Count > 0) throw new ArgumentException("treeView.SelectedNode.Nodes.Count > 0");
|
2024-09-04 16:40:39 +04:00
|
|
|
|
|
2024-09-19 23:40:13 +04:00
|
|
|
|
T val = new T();
|
|
|
|
|
TreeNode treeNode = treeView.SelectedNode;
|
|
|
|
|
int i = Levels.NodeNames.ToArray().Length - 1; // Получаем массив из очереди
|
|
|
|
|
while (treeNode != null && i >= 0)
|
|
|
|
|
{
|
|
|
|
|
PropertyInfo property = val.GetType().GetProperty(Levels.NodeNames.ToArray()[i]); // Обращаемся к массиву
|
|
|
|
|
if (property != null)
|
|
|
|
|
{
|
|
|
|
|
property.SetValue(val, Convert.ChangeType(treeNode.Text, property.PropertyType));
|
|
|
|
|
}
|
|
|
|
|
treeNode = treeNode.Parent;
|
|
|
|
|
i--;
|
|
|
|
|
}
|
|
|
|
|
if (i >= 0)
|
|
|
|
|
{
|
|
|
|
|
return null; // Не все свойства объекта установлены
|
|
|
|
|
}
|
|
|
|
|
return val;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-04 16:40:39 +04:00
|
|
|
|
}
|