119 lines
4.2 KiB
C#
119 lines
4.2 KiB
C#
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;
|
||
using System.Xml.Linq;
|
||
|
||
namespace VisableComponents
|
||
{
|
||
|
||
public partial class MyTreeView : UserControl
|
||
{
|
||
List<string> hierarchy = new List<string>();
|
||
public MyTreeView()
|
||
{
|
||
InitializeComponent();
|
||
}
|
||
public void addToHierarchy(List<string> list) { hierarchy = list; }
|
||
public int GetId
|
||
{
|
||
get {
|
||
if(treeView.SelectedNode.FirstNode == null)
|
||
return treeView.SelectedNode.Index;
|
||
return -1;
|
||
}
|
||
|
||
}
|
||
public object GetNode<T>(T nodeClass) where T: Type
|
||
{
|
||
if (hierarchy == null || hierarchy.Count == 0)
|
||
{
|
||
throw new Exception("Не задана иерархия");
|
||
}
|
||
if (treeView.SelectedNode?.FirstNode != null)
|
||
{
|
||
|
||
throw new Exception("Выбран не крайний элемент");
|
||
}
|
||
var res = Activator.CreateInstance(nodeClass);
|
||
T child;
|
||
TreeNode? parent = treeView.SelectedNode;
|
||
do
|
||
{
|
||
if (nodeClass.GetField(parent.Name) == null)
|
||
nodeClass.GetProperty(parent.Name).SetValue(res, parent.Text);
|
||
else nodeClass.GetField(parent.Name).SetValue(res, parent.Text);
|
||
parent = parent.Parent;
|
||
} while (parent != null);
|
||
return res;
|
||
}
|
||
public void LoadTree<T>(List<T> nodes)
|
||
{
|
||
if(hierarchy == null || hierarchy.Count == 0)
|
||
{
|
||
throw new Exception("Не задана иерархия");
|
||
}
|
||
treeView.Nodes.Clear();
|
||
foreach (T node in nodes)
|
||
{
|
||
TreeNode parent = null;
|
||
Type type = node.GetType();
|
||
foreach(string step in hierarchy)
|
||
{
|
||
var aa = type.GetFields();
|
||
if (type.GetField(step) != null || type.GetProperty(step) != null)
|
||
{
|
||
var value = type.GetField(step) == null ? type.GetProperty(step).GetValue(node) : type.GetField(step).GetValue(node);
|
||
TreeNode[] nodesLevel;
|
||
|
||
if (parent == null)
|
||
{
|
||
|
||
nodesLevel = treeView.Nodes.Find(step, false);
|
||
TreeNode currentNode = nodesLevel.FirstOrDefault(x => x.Text.Equals(value));
|
||
if (currentNode is null)
|
||
{
|
||
TreeNode newNode = new TreeNode(value.ToString());
|
||
newNode.Name = step;
|
||
treeView.Nodes.Add(newNode);
|
||
parent = newNode;
|
||
}else
|
||
{
|
||
parent = currentNode;
|
||
continue;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
|
||
nodesLevel = parent.Nodes.Find(step, false);
|
||
TreeNode currentNode = nodesLevel.FirstOrDefault(x => x.Text.Equals(value));
|
||
if (currentNode is null)
|
||
{
|
||
TreeNode newNode = new TreeNode(value.ToString());
|
||
newNode.Name = step;
|
||
parent.Nodes.Add(newNode);
|
||
parent = newNode;
|
||
}else
|
||
{
|
||
parent = currentNode;
|
||
continue;
|
||
}
|
||
}
|
||
|
||
}
|
||
else break;
|
||
|
||
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|